无界面!
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了乐清免费建站欢迎大家使用!
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class LinkCircle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n, k, m, i, j, count, p, q;
int[] a = new int[100];
ListInteger array = new LinkedListInteger();
p = 0;
q = 0;
count = 0;
n = in.nextInt();
m = in.nextInt();
for (i = 0; i n; i++) {
array.add(i + 1);
System.out.printf(i != n - 1 ? "%d " : "%d\n", array.get(i));
}
int pos = -1;
int x = 0;
while(array.size() != 1)
{
//x = array.get(pos);
pos += m ;
if(pos = array.size())
{
pos = pos % array.size();
}
System.out.println("remove pos:" + (pos + 1) + ", value:" + array.get(pos));
array.remove(pos--);
System.out.print("Array : ");
for (i = 0; i array.size(); i++) {
System.out.printf(i != array.size() - 1 ? "%d " : "%d\n\n", array.get(i));
}
}
System.out.println("res:" + array.get(0));
}
}
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数组实现约瑟夫环
package Josephround;
public class Joseround {
int sit;
int flagjo=0;
Joseround(){};
Joseround(int x){
sit=x;
}
void setflag(int x){
flagjo=x;
}
}
package Josephround;
public class Inijose {
Joseround jo[];
static int length=0;
Inijose(){};
Inijose(int x){
jo=new Joseround[x];
for(int i=0;ix;i++){
jo[i]=new Joseround(i+1);//创建对象数组
length++;
}
}
void delete(int n){
for(int i=n;ilength-1;i++){
jo[i]=jo[i+1];
}
length--;
}
}
package Josephround;
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
int m,n;
System.out.println("input m");
Scanner m1=new Scanner(System.in);
m=m1.nextInt();
System.out.println("input n");
Scanner n1=new Scanner(System.in);
n=n1.nextInt();
int temp=0;
int x=0;
Inijose joseph=new Inijose(n);
while(joseph.length!=0){
for(int i=1;i=m;i++){
joseph.jo[x].setflag(i);
if(joseph.jo[x].flagjo==m){
System.out.println(joseph.jo[x].sit);
joseph.delete(x);
x--;
}
if(xjoseph.length-1) x++;
else x=0;
}
}
}
}
精简易懂版本 刚写的
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();
}
}