牛客网编程练习之PAT乙级(Basic Level):1032 选大王
典型的约瑟夫环问题
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){ List<Integer> list = 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); } }
题目来源: https://www.nowcoder.com/pat/2/problem/373
.