约瑟夫java设计大作业

这是17世纪的法国数学家加斯帕在《数目的游戏问题》中讲的一个故事:15个教徒和15 个非教徒在深海上遇险,必须将一半的人投入海中,其余的人才能幸免于难,于是想了一个办法:30个人围成一圆圈,从第一个人开始依次报数,每数到第九个人 就将他扔入大海,如此循环进行到仅余15个人为止。问怎样排 法,才能 的使每次投入大海的都是非教徒
的设计思想,java设计大作业

//a代表教徒,b代表非教徒
public class Test {

/**
 * Enter Content.
 * 
 * @author zhangjing
 * @date 2015-5-19
 * @param args
 * @see
 */
private HashMap map = new HashMap();
public Test(){
    for(int i= 1 ; i<=30;i++){
        map.put(i, "a");
    }
}
private int now = 1;

public void next() {
     if(now==30){
         now=1;
     }else{
         now++;
     }
}


public static void main(String[] arg){
    Test t = new Test();
    int num = 30;
    while(num>15){
        for(int i= 1;i<=9;i++){
            t.next();
            if(i==9){
                t.map.put(t.now, "b");
                num--;
            }
        }
    }
    System.out.println(t.map.values().toString());
}

}
结果:[b, a, a, b, a, a, b, a, a, b, a, a, b, a, a, a, b, b, a, a, a, a, b, b, a, a, a, a, b, a]

一个 Java,和一个 C 的,供参考:

Java 解决约瑟夫问题

15个教徒和15 个非教徒在深海上遇险,必须将一半的人投入海中,其余的人才能幸免于难,于是想了一个办法

http://zuoye.baidu.com/question/0c0f6750ff5266617720849c43424417.html

http://www.cnblogs.com/timeng/p/3335162.html

public class Test {

public static void main(String[] args) {
    Boolean[] userBoolean = new Boolean[30];
    for (int i = 0; i < userBoolean.length; i++) {
        userBoolean[i] = true;
    }
    for (int i = 0; i < userBoolean.length; i++) {
        System.out.print(i+"="+userBoolean[i]+"  ");
    }
    System.out.println();
    int count = 0;
    int length = userBoolean.length;
    int index = 0;

    while (length >15) {
        count ++ ;
        if (count == 9) {
            count = 0 ;
            while (!userBoolean[index]) {
                index ++ ;
            }
            userBoolean[index] = false;
            length -- ;
        }
        index ++;
        if(index == userBoolean.length) index = 0;
    }
    for (int i = 0; i < userBoolean.length; i++) {
        System.out.print(i+"="+userBoolean[i]+"  ");
    }
}

}

先不考虑教徒和非教徒的关系 30个人直接扔,扔完以后倒推就可以确定非教徒的位置了,就是丢手帕