当前位置:网站首页>Simulate the hundred prisoner problem
Simulate the hundred prisoner problem
2022-07-05 18:03:00 【Memory condensation】
#include <iostream>
#include <time.h>
using namespace std;
const int N = 100;
int dfs(int a[], int x, int vis[]) {
if (vis[a[x]]) {
return 0;
}
vis[a[x]] = 1;
return dfs(a, a[a[x]], vis) + 1;
}
int main() {
srand(time(0));
int cnt = 0, T = 100000;
for (int round = 0; round < T; round++) {
int a[128] = {0};
for (int i = 1; i <= N; i++) {
a[i] = i;
}
for (int i = N; i > 1; i--) {
int r = 1 + rand() % i;
int t = a[i];
a[i] = a[r];
a[r] = t;
}
int vis[128] = {0};
for (int i = 1; i <= N; i++) {
if (dfs(a, i, vis) > (N >> 1)) {
cnt++;
break;
}
}
}
cout << 1.0 * cnt / T;
return 0;
}
边栏推荐
猜你喜欢
ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测
LeetCode 练习——206. 反转链表
记一次使用Windbg分析内存“泄漏”的案例
“12306” 的架构到底有多牛逼?
What are the requirements for PMP certification? How much is it?
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
使用QT遍历Json文档及搜索子对象
Image classification, just look at me!
U-Net: Convolutional Networks for Biomedical Images Segmentation
2022新版PMP考试有哪些变化?
随机推荐
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
论文阅读_中文NLP_LTP
GFS分布式文件系统
“12306” 的架构到底有多牛逼?
Sophon autocv: help AI industrial production and realize visual intelligent perception
每日一练:关于日期的一系列
mybash
深拷贝与浅拷贝【面试题3】
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
Ten capabilities that cyber threat analysts should have
LeetCode每日一题:合并两个有序数组
leetcode每日一题:字符串中的第一个唯一字符
Cmake tutorial Step4 (installation and testing)
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
从XML架构生成类
消除`if()else{ }`写法
Disabling and enabling inspections pycharm
Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
多线程(一) 进程与线程
Introduction to VC programming on "suggestions collection"