九度 1188 约瑟夫环问题

1、原生的约瑟夫环问题,编号从1开始,共有N个人,步长为M,最后一个剩下的人的编号

http://ac.jobdu.com/problem.php?pid=1188

这里输出的是每一次出局人的编号

public static void calc(int n, int m) {
        boolean[] markArr = new boolean[n + 1];
        for (int i = 0; i < n; i++) {
            markArr[i] = true;
        }

        int step = 0, start = 0, count = 0;
        for (int i = start;; i++) {
            if(i == n){
                i = 0;
            }
            if (markArr[i]) {
                step++;
            }
            if (step == m) {
                markArr[i] = false;
                count++;
                if (count == n) {
                    System.out.println(i + 1);
                    break;
                }else{
                    System.out.print(i + 1 + " ");
                }
                step = 0;
            }
        }
    }

 

posted @ 2016-05-06 12:48  qingyezhu  阅读(229)  评论(0)    收藏  举报