约瑟夫环问题

/*
 *约瑟夫环问题
 * 抱1 2 3
 */
import java.util.*;
public class Yuan{

    public static void main(String[] args) throws Exception{
        Scanner input = new Scanner(System.in);
        int p = input.nextInt();
        System.out.println(leftPerson(p));
    }
    public static int leftPerson(int n) throws Exception {
        boolean[] per = new boolean[n];
        for (int i = 0; i < per.length; i++) {
            per[i] = true;
        }
        int t = 0, len = per.length;
        while (len > 1) {
            for (int i = 0; i < per.length; i++) {

                if (per[i]) {
                    t++;
                    if (t == 3) {
                        t = 0;
                        per[i] = false;
                        len--;
                    }
                }
            }
        }
        int a = 0;
        for(int j = 0;j<per.length;j++){
            if(per[j]=true){
                a = j;
            }
        }
        return a;
    }
}

 

posted on 2015-10-11 15:48  上发条的姑娘  阅读(146)  评论(0编辑  收藏  举报

导航