Description
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.
Input
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).
Output
Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.
Sample Input
7 5 10 4 11 4 1
4 2 5 6 1
3 2 2 5
3 2
有n个人,第一个人是领导,k个数,每个数代表要走的步数,最后一步到达的地方删除这个人,这个人的下一个人是新领导,看走完k组数,每个删除的序号是几
分析:
用vector储存编号,用p标记删除的位置,对n求膜求删除的位置
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
long int n,b[105]={0},i,j,t,m,p=0;
vector<int>v;
cin>>n>>m;
for(i=1;i<=n;i++)
v.push_back(i);
for(i=1;i<=m;i++){
cin>>t;
p=(p+t)%n;
b[i]=v[p];
for(j=p;j<n-1;j++)
v[j]=v[j+1];
v.pop_back();
n--;
}
for(i=1;i<m;i++)
cout<<b[i]<<" ";
cout<<b[m]<<endl;
}
感受:
如果不删除该删除的部分,容易超时。。。第一次提交就超时了