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
Hint
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.
题目意思:
n个人围成一圈坐着,每个人(编号定为1~n)有一个数字a(限定条件:a的范围为1~n,且每个数字只出现一次),
现在如果指定一个人为leader,下一个leader为从他的顺时针方向第一个人开始数 a .以此类推,现在给出一系列按顺序成为leader的人的编号,
问是否存在这样一种情况,能够在限定条件内确定每个人的数字,满足成为leader顺序的结果,如果有多种结果,输出一种,没有输出 -1。
解题思路:这其实就是一道约瑟夫环问题
1 #include<stdio.h> 2 #include<deque> 3 #include<string.h> 4 using namespace std; 5 int main() 6 { 7 int n,k,i,j,b[100],a,c; 8 deque<int> q; 9 scanf("%d%d",&n,&k); 10 for(i=1;i<=n;i++) 11 { 12 q.push_back(i); 13 } 14 for(i=1;i<=k;i++) 15 { 16 scanf("%d",&b[i]); 17 } 18 for(i=1;i<=k;i++) 19 { 20 b[i]=b[i]%n; 21 for(j=1;j<=b[i];j++) 22 { 23 a=q.front(); 24 q.pop_front(); 25 q.push_back(a); 26 } 27 c=q.front(); 28 printf("%d ",c); 29 q.pop_front(); 30 n--; 31 } 32 return 0; 33 }