64,282
社区成员
发帖
与我相关
我的任务
分享
......
counter = rand() % 100;
bool flag = false;
prisoner.set(counter);
bitset<100> prisoner_out(0);
prisoner_out.set(counter);
while (true)
{
++day;
i = rand() % 100;
if (!prisoner_out[i])
{
prisoner_out.set(i);
if (prisoner_out.count() == prisoner_out.size())
{
cout << "All Out :" << day << "days\t\t" << day/365 << "years" << endl;
cout << "But they don't know! until ..." << endl;
}
}
if (flag && i == counter)
......
/*参考资料:http://www.javaeye.com/topic/569275*/
////////////////////////////////////////////////////////////
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
////////////////////////////////////////////////////////////
int main()
{
bitset<100> prisoner(0);
int day = 1;//已过了的天数
int counter;//统计者
int close_count = 0;//已经关灯次数
int i;//随机放风的囚犯
srand((unsigned)time(NULL));
counter = rand() % 100;//第一天的那个囚犯为统计者
bool flag = false;//灯的状态
prisoner.set(counter);//统计囚犯设置为已经统计过
while (true)
{
++day;
i = rand() % 100;
if (flag && i == counter)
{//灯是开的,且是统计囚犯
flag = false;
++close_count;
}
else if (!flag && !prisoner[i])
{//灯是关的,且是未统计过的囚犯
flag = true;
prisoner.set(i);
}
if (close_count == 99)
{
break;
}
}
cout << "Out of prison:" << day << "days\t\t" << day/365 << "years" << endl;
system("pause");
return 0;
}
第三种方法提示如下:
分为一个基本记数者和N个2级记数者,基本记数者的独立记数上限Cp和2级记数的独立计数上限Cs,时间由长度为S1和S2的两段交替组成.在S1阶段,计数者计数直到达到其独立计数上限,在S2阶段,达到计数上限的2级计数者将通过开灯方法告诉基本计数者直到基本计数者得到所有2级计数者完成任务并加上自己计数为100时宣布自由。
此方法通过计算机模拟优化可在10年左右完成 。