约瑟夫问题

/*
 * 说有15个教徒和15个非教徒在海上遇难,必须将一半的人投入海中,其余的人才能幸免于难。于是他们想了一个办法:30个人围成一圈,从第一个人开始报数,每数到第九个人的时候将之投入大海,如此循环直到剩下15个人为止。问:怎样的排法可以使被投入大海的都是非教徒?
 *
 * StoryMonster    2016/7/14
 */
#include <iostream>

using namespace std;

enum {BELIEVER=0,NONBELIEVER};
int main()
{
    unsigned short people[30] = {BELIEVER};
    unsigned short surviver = 30;
    unsigned short index = 0;
    while(surviver > 15)
    {
        unsigned short count = 0;
        while(1)
        {
            if(people[index] == BELIEVER)
            {
                count++;
                if(count == 9) break;
            }
            index++;
            if(index == 30) index = 0;
        }
        people[index] = NONBELIEVER;
        surviver--;
    }
    for(index=0;index<30;index++)
    {
        if(people[index] == BELIEVER)
            cout<<"BELIEVER  ";
        else cout<<"NONBELIEVER  ";
    }
    cout<<endl;
    return 0;
}
发布了65 篇原创文章 · 获赞 9 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章