首页 > 其他 > 详细

约瑟夫环问题

时间:2015-10-11 16:26:45      阅读:180      评论:0      收藏:0      [点我收藏+]
/*
 *约瑟夫环问题
 * 抱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;
    }
}

 

约瑟夫环问题

原文:http://www.cnblogs.com/sumbud/p/4869407.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!