约瑟夫环问题

https://blog.csdn.net/u011500062/article/details/72855826

import java.util.ArrayList;

public class Solution {

    public static void main(String[] args) {
        int result = get(5,2);
        System.out.println(result);
    }
    public static int get(int n, int m){
        ArrayList<Integer> list = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            list.add(i+1);
        }
        int idx = 0;
        int temp = m;
        while (n > 1) {
            idx = (idx + temp - 1) % n;
            temp = temp*m;
            list.remove(idx);
            n--;
        }
        return list.get(0);
    }
}

约瑟夫环问题

原文地址:https://www.cnblogs.com/LoganChen/p/13720887.html