题面
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.
Examples
input
7 5
10 4 11 4 1
output
4 2 5 6 1
题目大意
有n个孩子在围成一圈做游戏,编号为1-n。一共有K步操作,一开始默认领导人是1。然后第i步数ai个人,这个孩子退场。退场孩子的下一个孩子接任领导人。求每一步的退场人的编号。
大致思路
经典的约瑟夫环问题。不要想得太过复杂。但由于ai可能达到1e9的级别,所以需要对现有人数取%。每有一个孩子退场,把后面孩子的编号都向前移一位。这样方便进行处理。(用bool数组进行判断反而比较麻烦,而且容易出错)
代码:
#include<iostream>
using namespace std;
int main()
{ios::sync_with_stdio(false);int a[110],n,k;while(cin>>n>>k){int x=0,num;for(int i=1;i<=n;++i)a[i]=i;//编号for(int i=1;i<=k;++i){cin>>num;x+=num;x%=n;//x代表着每次出局的人if(i!=1)cout<<" ";cout<<a[x+1];--n;for(int j=x+1;j<=n;++j)a[j]=a[j+1];//后面孩子全部前移一格}}return 0;
}