当前位置:网站首页>模拟百囚徒问题
模拟百囚徒问题
2022-07-05 17:50:00 【忆灬凝】
#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基于边界网的云检测
- Cmake tutorial Step2 (add Library)
- Interpretation: how to deal with the current security problems faced by the Internet of things?
- C # mixed graphics and text, written to the database in binary mode
- Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
- Easynmon Usage Summary
- Daily exercise: a series of dates
- Disorganized series
- Size_ T is unsigned
- What are the requirements for PMP certification? How much is it?
猜你喜欢
随机推荐
ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
VBA drives SAP GUI to realize office automation (II): judge whether elements exist
Teamcenter 消息注册前操作或後操作
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
消除`if()else{ }`写法
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
论文阅读_中文NLP_LTP
寻找第k小元素 前k小元素 select_k
怎么选择外盘期货平台最正规安全?
Teamcenter 消息注册前操作或后操作
Count the running time of PHP program and set the maximum running time of PHP
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
Action avant ou après l'enregistrement du message teamcenter
ELK日志分析系统
Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
求解为啥all(())是True, 而any(())是FALSE?
Why is all (()) true and any (()) false?
MATLAB查阅
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection