题解 | 约瑟夫环

约瑟夫环

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就比数组清晰很多了

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务