逆序向约瑟夫环

2019-04-13 16:09发布

约瑟夫环,是模n 删除,这里的题解是 模n 插入 这里的例子是 模 2 public static void main(String args[]) { Scanner cin = new Scanner(System.in); int x = cin.nextInt(); int[] a = new int[x]; for (int i = 0; i < x; ++i) { a[i] = cin.nextInt(); if (a[i] == 1) { System.out.println(1); } else if (a[i] == 2) { System.out.println("2 1"); } else { int[] b = new int[a[i]]; int index = 0; int space = 0; int j=1; while (j != a[i]) { index=index%a[i]; if (space == 0) { if (b[index] == 0) { space++; index++; } else index++; } else if (space == 1) { if (b[index] == 0) { b[index] = j; j++; index++; space=0; } else index++; } } for(int p=0;p