题解 | 约瑟夫环
约瑟夫环
https://www.nowcoder.com/practice/e417cfe32c74416ca38247f619ddb322
import java.util.ArrayList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int m = sc.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(i); } while (list.size() > 1) { k = (k + m - 1) % list.size(); list.remove(k); } if (list.size() == 1) { System.out.println(list.get(0)); } } }
用ArrayList就比数组清晰很多了