Hust oj 1548 基础数据结构——循环链表
2016-05-22 22:23
316 查看
基础数据结构——循环链表 | ||||||
| ||||||
Description | ||||||
n个人想玩残酷的死亡游戏,游戏规则如下: n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。 请输出最后一个人的编号。 | ||||||
Input | ||||||
输入n和m值。m>1。 | ||||||
Output | ||||||
输出胜利者的编号。 | ||||||
Sample Input | ||||||
5 3 | ||||||
Sample Output | ||||||
4 | ||||||
Hint | ||||||
第一轮:3被杀第二轮:1被杀第三轮:5被杀第四轮:2被杀 讲道理就是一个循环链表。。我用循环队列做的 #include<cstdio> #include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int maxn = 1005; int vis[maxn]; int a[maxn]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { queue<int>q; for(int i=1;i<=n;i++) q.push(i); int cnt = 0; while(q.size() > 1) { cnt++; if(cnt == m) { q.pop(); cnt = 0; } else { q.push(q.front()); q.pop(); } } printf("%d\n",q.front()); } return 0; } |
相关文章推荐