典型的约瑟夫环问题
AC代码:
import java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * @author CC11001100 */public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ System.out.println(joseph(sc.nextInt(), sc.nextInt())); } } private static int joseph(int n, int m){ Listlist = new ArrayList<>(); for(int i=1; i<=n; i++){ list.add(i); } int last = -1; while(list.size()>1){ last = (last + m) % list.size(); list.remove(last); last--; } return list.get(0); }}
题目来源:
.