public class Sefu {
public static void main(String[] args) {
int N// 总人数
int M// 从第M个人开始
int S// 隔S个人出局
Scanner reader = new Scanner(System.in)
System.out.print("请输入N:")
N = reader.nextInt()
System.out.print("请输入M:")
M = reader.nextInt()
System.out.print("请输入S:")
S = reader.nextInt()
int[] a = new int[N]
for (int h = 1 h <= N h++) {
a[h - 1] = h
System.out.print(a[h - 1] + " ")
}
System.out.println()
int count
M = M - 1// 转成下标
for (int j = 1 j <= N j++) {
count = 1// 从1开始报数
while (count != S) {// 如果没有报到S就继续报数
if (a[M] != 0) {// 如果没出局就报数
count++//报数
for (int i = 1 i < N i++) {
//然后找下一个(从+1开始找)要报数的人
if (M + i < N) {
//如果M+i没到最后一位
if (a[M + i] != 0) {
//判断是否出局
M = M + i
//没出局就是从这个人开始了
break
//不要找了
}else{
continue
//出局了就i++,看下一个人有没有出局
}
}else{
if (a[M + i-N] != 0) {
//如果M+i过了最后一位,就-N,相当于转了一个圈回到前面
M = M + i-N
//没出局就从M+i-N开始报数
break
}else{
continue
}
}
}
} else {// 如果出局了就跳过找下一个要报数的人
if (M + 1 < N) {
M++
} else {
M = 0
// 如果M+1==N就从下标0开始找数组的值不是0的数
continue
}
continue// 如果是0就继续
}
}
// 如果报数报到S
System.out.print(a[M] + " ")// 出局
int out=M//下标为out的出局了
for (int i = 0 i < N i++) {//找下一个要报数的人
if (M + i < N) {//如果M+i没到最后一位
if (a[M + i] != 0) {//判断是否出局
M = M + i
break
}else{
continue//出局了就i++从下一个开始
}
}else{
if (a[M + i-N] != 0) {
//如果M+i过了最后一位,就-N,相当于转了一个圈回到前面
M = M + i-N
break
}else{
continue
}
}
}
a[out]=0//出局的设为0
}
}
}
//写完发现我定义的S和M和你定义的是反过来的,不是最好的算法。
//可以把找下一个人的方法抽出来,写成一个方法
//输入一个数组a,找第M个人的下一个人,输出下一个人的下标。
import java.util.*public class Sefu {
public static void main(String[] args) {
int N// 总人数
int M// 从第M个人开始
int S// 隔S个人出局
Scanner reader = new Scanner(System.in)
System.out.print("请输入N:")
N = reader.nextInt()
System.out.print("请输入M:")
M = reader.nextInt()
System.out.print("请输入S:")
S = reader.nextInt()
int[] a = new int[N]
for (int h = 1 h <= N h++) {
a[h - 1] = h
System.out.print(a[h - 1] + " ")
}
System.out.println()
int count
M = M - 1// 转成下标
for (int j = 1 j <= N j++) {
count = 1// 从1开始报数
while (count != S) {
// 如果没有报到S就继续报数
count++// 报数
M=getNext(a, M)
//找下一个没出局(不为0)的数
}
// 如果报数报到S
System.out.print(a[M] + " ")// 出局
a[M]=0
M=getNext(a, M)//找下一个没出局的数
}
}
/**
* 得到下一个数的下标
* @param a
* @param M
* @return
*/
public static int getNext(int[] a, int M) {
for (int i = 1 i < a.length i++) {
if (M + i < a.length) {
if (a[M + i] == 0) {
//如果下一个数的值为0就继续找
continue
} else {
M=M+i
return M
//如果下一个数的值不为0就是这个数。
}
}else{
if (a[M + i-a.length] == 0) {
//如果下一个数的值为0就继续找
continue
} else {
M=M+i-a.length
return M
//如果下一个数的值不为0就是这个数。
}
}
}
return -1
}
}
//一不小心又帮你改好了~
用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=0i<xi++){
jo[i]=new Joseround(i+1)//创建对象数组
length++
}
}
void delete(int n){
for(int i=ni<length-1i++){
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=1i<=mi++){
joseph.jo[x].setflag(i)
if(joseph.jo[x].flagjo==m){
System.out.println(joseph.jo[x].sit)
joseph.delete(x)
x--
}
if(x<joseph.length-1) x++
else x=0
}
}
}
}
精简易懂版本 刚写的public class Yuesefu {
// 数三退一 约瑟夫环 求最后留在环中小朋友的位置 即原来的编号(1-500)
/**
* 初始化数组,添加标号
*/
private void markID(int[] child) {
for (int i = 0i <= child.length - 1i++) {
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()
}
}