n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from1 to n. In the beginning, the first child is considered the leader. The game is played ink steps. In the i-th step the leader counts outai 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 child13 and ai = 12, then counting-out rhyme ends on child16, 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 andk (2 ≤ n ≤ 100,1 ≤ k ≤ n - 1).
The next line contains k integer numbersa1, a2, ..., ak (1 ≤ ai ≤ 109).
Print k numbers, the i-th one corresponds to the number of child to be eliminated at thei-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, child3 becomes the leader.
- In the third step child 5 is eliminated, child 6 becomes the leader.
- In the fourth step child 6 is eliminated, child7 becomes the leader.
- In the final step child 1 is eliminated, child 3 becomes the leader.
原题链接:http://codeforces.com/problemset/problem/792/B
第一次在codeforces上做题,算是一个不错的开始.
题意:一群编号为1-N的人按顺时针围成一个圈。开始位置为1,然后给出K个数字a1~ak,分成K次执行,执行第i次时,从开始位置顺时针数ai个数,输出且删除数到的最后一个数,然后开始位置变为被删除的数的下一个数。
样例1解析:
7 5
10 4 11 4 1
4 2 5 6 1
起点为1,数10个位置,结果为4,输出and删除4。
起点变成5,数4个位置,结果为2,输出and删除2。
起点变成3....后面自己推吧= =
思路:用一个数组标记 数 是否已经被删除,若被删除,数的时候直接跳过。
要注意的是:因为测试数据给出的数是比较大的,所以要先进行求余,如果不这样做,TLE...
自己用的是数组实现,其实这样并不好,处理的时候很麻烦,看下面代码就知道了。......强烈建议用单向循环链表写。
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
#define MAXN 105
bool del[MAXN];
int main()
{
int n,m;
int a;
int p;
while(cin>>n>>m)
{
p=2;//起点是1,所以从2开始数
int nn=n;
memset(del,false,sizeof(del));
while(m--)
{
cin>>a;
if(a%nn)a%=nn;//余数不为0时,求余
else if(a>nn)//余数为0且a>nn时,直接等于nn;
{
a=nn;
}
nn--;
while(1)
{
if(!del[p])
{
a--;
if(a==0)
{
del[p]=true;
break;
}
}
p++;
if(p>n)p=1;
}
if(m!=0)cout<<p<<" ";
else cout<<p;
while(del[p])//找起点
{
p++;
if(p>n)p=1;
}
//找起点的下一个数,因为是从起点的下一个数开始数的
p++;
if(p>n)p=1;
while(del[p])
{
p++;
if(p>n)p=1;
}
}
cout<<endl;
}
}