/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
/**
*
* @author Administrator
*/
import java.util.Scanner;
public class 约瑟夫环 {
public static void main(String[] args) {
System.out.println("约瑟夫环问题");
System.out.print("输入人数");
Scanner sca = new Scanner(System.in);
int m = sca.nextInt();//人数 m
System.out.print("要出圈的号");
int n = sca.nextInt();//出圈的数 n
System.out.println("按要出圈的次序输出序号");
int a[] = new int[m];
int len = m;
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
}
int i = 0;
int j = 1;
while (len > 0) {
if (a[i % m] > 0) {
if (j % n == 0) {
System.out.print(a[i % m] + " ");
a[i % m] = 0 - 1;
j = 1;
i++;
len--;
} else {
i++;
j++;
}
} else {
i++;
}
}
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
/**
*
* @author Administrator
*/
import java.util.Scanner;
public class 约瑟夫环 {
public static void main(String[] args) {
System.out.println("约瑟夫环问题");
System.out.print("输入人数");
Scanner sca = new Scanner(System.in);
int m = sca.nextInt();//人数 m
System.out.print("要出圈的号");
int n = sca.nextInt();//出圈的数 n
System.out.println("按要出圈的次序输出序号");
int a[] = new int[m];
int len = m;
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
}
int i = 0;
int j = 1;
while (len > 0) {
if (a[i % m] > 0) {
if (j % n == 0) {
System.out.print(a[i % m] + " ");
a[i % m] = 0 - 1;
j = 1;
i++;
len--;
} else {
i++;
j++;
}
} else {
i++;
}
}
}
}