import java.util.ArrayList;
import java.util.List;
public class YSF {
private int number;
private int key;
List<Integer> list = new ArrayList<Integer>();
public YSF(int number,int key){
this.number = number;
this.key = key;
for (int i = 1; i <= this.number;i++) {
list.add(i);
}
System.out.println("初始的约瑟夫环为:");
for (int i = 0; i < list.size(); i++) {
System.out.print(" " + list.get(i));
}
System.out.println();
int count = 1;
for(int i = 0;list.size() != 0;){
if(count == key){
System.out.println(list.get(i));
list.remove(i);
count = 1;
}
else
{
count++;
i = (++i) % list.size();
}
}
}
}
package LiTi;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
System.out.println("请输入人数和key值");
int number = 0;
Scanner input = new Scanner(System.in);
number = input.nextInt();
int key = input.nextInt();
YSF ysf = new YSF(number,key);
}
}
|