击鼓传花 题解:鄙人认为,约瑟夫环问题用队列就是最容易理解的
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
queue<int> q;
for(int i = 1; i <= n; i ++){
q.push(i);
}
while(q.size() > 1){
for(int i = 0; i < 2; i ++){
q.push(q.front());
q.pop();
}
q.pop();
}
cout << q.front();
return 0;
}
登录后发布评论
暂无评论,来抢沙发