n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.
For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.
You have to write a program which prints the number of the child to be eliminated on every step.
The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1).
The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).
Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.
7 5 10 4 11 4 1
4 2 5 6 1
3 2 2 5
3 2
Let's consider first example:
- In the first step child 4 is eliminated, child 5 becomes the leader.
- In the second step child 2 is eliminated, child 3 becomes the leader.
- In the third step child 5 is eliminated, child 6 becomes the leader.
- In the fourth step child 6 is eliminated, child 7 becomes the leader.
- In the final step child 1 is eliminated, child 3 becomes the leader.
题意:模拟链表组成一个圈,k次数数,从第一个开始数,数到哪个删哪个;
分析:用vector模拟一下就好
AC code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1
const int maxn=100000+50;
vector<int> a;
void init(int n){
a.clear();
for(int i=1;i<=n;i++){
a.push_back(i);
}
}
int main() {
int n,k;
while(~scanf("%d%d",&n,&k)){
init(n);
int leader=0;
vector<int>::iterator it;
for(int i=1;i<=k;i++){
int num;
scanf("%d",&num);
leader=(leader+num)%a.size();
it=a.begin()+leader;
printf("%d%c",*it,(i==k)?'\n':' ');
a.erase(it);
}
}
}

本文介绍了一个计数游戏的模拟问题,孩子们围成一圈并按特定规则淘汰玩家,直至最后。文章详细阐述了游戏规则,并提供了使用C++实现的代码示例。
379

被折叠的 条评论
为什么被折叠?



