java约瑟夫环简单代码,约瑟夫环java简单实现

http://www.itjxue.com  2023-01-13 20:46  来源:未知  点击次数: 

java编程序实现约瑟夫环

精简易懂版本 刚写的

public class Yuesefu {

// 数三退一 约瑟夫环 求最后留在环中小朋友的位置 即原来的编号(1-500)

/**

* 初始化数组,添加标号

*/

private void markID(int[] child) {

for (int i = 0; i = child.length - 1; i++) {

child[i] = i + 1;

}

}

/**

* 求出小孩子原来的位置在什么地方,从一开始

*/

public void re_index(int[] i) {

for (int ii : i) {

if (ii 0) {

System.out.println("还在场内的小朋友的序号为 : " + ii);

}

}

}

/**

* 统计剩余的同学

*/

public int record(int[] re) {

int reco = 0;

for (int i : re) {

if (i 0) {

reco = reco + 1;

} else {

continue;

}

}

// System.out.println("目前还剩下:" + reco + " 个小朋友在圈圈里面");

System.out.println("程序努力加载计算中.............................................................................................." + reco);

return reco;

}

public static void run() {

int childs[] = new int[500];

Yuesefu yuesefu = new Yuesefu();

yuesefu.markID(childs);

yuesefu.startGame(childs);

yuesefu.re_index(childs);

}

/**

* 游戏开始

*/

private void startGame(int[] i) {

int index = 0; // 用作循环

int leave = 0; // 用作离场

while (true) {

if (i[index] != 0) {// 还在场内

leave = leave + 1; // 报数

if (leave == 3) {// 判断是否需要离场 需要

i[index] = 0; // 离场

leave = 0;// 重新报数

}

index = index + 1;// 下一个人预备

if (index == i.length) {// 已经到最后了 需重新循环

index = 0;

}

} else {// 不在场内

if (index == i.length - 1) {// 判断此出场的人是否为最后一个人

index = 0;

}

index = index + 1;// 跳过

}

int x = new Yuesefu().record(i);// 统计剩余人数

if (x == 1)

break;

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

run();

}

}

急!急!急!Java怎么用递归实现约瑟夫环?求试验成功的、初级的代码

public?class?TestJosephus?{

public?static?void?main(String[]?args)?{

//留几个人

int?alive?=?2;

//总人数

int?total?=?41;

//自杀者报数

int?killMan?=?3;

Josephus(alive,?total,?killMan);

}

/**

?*?@param?alive?存活的人初始位置序号//留几个人

?*?@param?total?总人数

?*?@param?killMan?自杀者报数

?*/

public?static?void?Josephus(int?alive,?int?total,?int?killMan)?{

int?[]man?=?new?int[total];

int?count?=?1;

int?i?=?0;

int?pos?=?-1;

while?(count?=?total)?{

do?{

pos?=?(pos+1)%total;

if?(man[pos]?==?0)?{

i++;

}

if?(i?==?killMan)?{

i?=?0;

break;

}

}?while?(true);

man[pos]?=?count;

System.out.print("第?"?+?(pos+1)?+?"?个人自杀!约瑟夫环编号为:"?+?man[pos]);

if?(count?%?2?!=?0)?{

System.out.print("?-?");

}else?{

System.out.println("?-=?");

}

count++;

}

System.out.println();

System.out.println("这?"?+?alive?+"?个需要存活的人初始位置应排在以下序号:");

alive?=?total?-?alive;

for?(i?=?0;?i??total;?i++)?{

if?(man[i]??alive)?{

System.out.println("初始编号:"?+?(i+1)?+?",约瑟夫环编号:"?+?man[i]);

}

}

System.out.println();

}

}

急求用java解决约瑟夫环的编程(接图片“显示“出环者”次序并给出最终的赦免者”)

import?java.util.ArrayList;??

import?java.util.List;??

import?java.util.Scanner;??

??

public?class?demo?{??

????public?static?void?main(String[]?args)?{??

????????Scanner?scanner?=?new?Scanner(System.in);??

????????System.out.print("请输入总人数:");??

????????int?totalNum?=?scanner.nextInt();??

????????System.out.print("请输入报数的大小:");??

????????int?cycleNum?=?scanner.nextInt();??

????????System.out.print("请输入第几个报数:");?

????????int?cur?=?scanner.nextInt();??

????????yuesefu(totalNum,?cycleNum?,cur);?

????????scanner.close();

????}??

??

???public?static?void?yuesefu(int?totalNum,?int?countNum,int?cur)?{??

????????//?初始化人数??

????????ListInteger?start?=?new?ArrayListInteger();??

????????for?(int?i?=?1;?i?=?totalNum;?i++)?{??

????????????start.add(i);??

????????}??

????????//从第K个开始计数??

????????int?k?=?cur-1;??

????????while?(start.size()?0)?{??

????????????k?=?k?+?countNum;??

????????????//第m人的索引位置??

????????????k?=?k?%?(start.size())?-?1;??

???????????//?判断是否到队尾??

????????????if?(k??0)?{??

????????????????System.out.println(start.get(start.size()-1));??

????????????????start.remove(start.size()?-?1);??

????????????????k?=?0;??

????????????}?else?{??

????????????????System.out.println(start.get(k));??

????????????????start.remove(k);??

????????????}??

????????}??

????}??

}

(责任编辑:IT教学网)

更多

推荐CSS教程文章