java-解决约瑟夫问题
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
// 约瑟夫问题12345 -5 6=1 7=2
// 约瑟夫函数第一个参数是总人数,第二个参数是第几人报数后出局
public class Work19_3_15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
yue_se_fu(input,5);
scanner.close();
}
public static void yue_se_fu(int input,int y){
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= input; i++) {
list.add(i);
}
int num = 0;
while (list.size()>0){
num = num + y;
num = num % list.size()-1;
if (num<0){
System.out.println(list.get(list.size()-1));
list.remove(list.size()-1);
num = 0;
}else {
System.out.println(list.get(num));
list.remove(num);
}
}
}
}