'commit'
This commit is contained in:
25
TangDou/KaoShi/约瑟夫环经典版.cpp
Normal file
25
TangDou/KaoShi/约瑟夫环经典版.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
int n;
|
||||
queue<int> q;
|
||||
int cnt; //当前轮次第几个
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
//为啥这里把所有人员都入队列呢?当面要解释清楚
|
||||
for (int i = 1; i <= n; i++) q.push(i);
|
||||
//如果还剩余大于1个
|
||||
while (q.size() > 1) {
|
||||
//取出一个
|
||||
int t = q.front();
|
||||
q.pop();
|
||||
cnt++;//点了一个数
|
||||
|
||||
if (cnt == 3) cnt = 0;
|
||||
else q.push(t);
|
||||
}
|
||||
//输出最后一个
|
||||
cout << q.front();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user