数组模拟环进行删除操作的例题
2017-03-29 13:22
253 查看
B. Counting-out Rhyme
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
output
input
output
Note
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.
这道题本不想写博客,但想起这类型的题很多,就算是写个板子吧。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
input
3 2 2 5
output
3 2
Note
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.
这道题本不想写博客,但想起这类型的题很多,就算是写个板子吧。
#include<bits/stdc++.h> using namespace std; int n,k; int len=n; int a[105]; int b[105]; void weiyi(int pos) { int i,j; for(i=pos;i<len;i++) { a[i]=a[i+1]; } } int main() { cin>>n>>k; int i,j; for(i=1;i<=k;i++) scanf("%d",&b[i]); for(i=0;i<n;i++) a[i]=i+1; int t=1; int step; int pos=0; len=n; while(k--) { step=b[t++]; pos=(pos+step)%len; printf("%d ",a[pos]); weiyi(pos); len--; } return 0; }
相关文章推荐
- 使用memmove对数组中的指定内容进行删除操作
- 这是一个定时脚本,主要功能是遍历该文件夹下的所有文件并存储到数组,对数据中的文件进行操作,一个一个移动到指定的目录下,并删除原有文件
- 在数组的首尾进行删除一项或者是添加一项操作
- 以forin的方式遍历数组时进行删除操作的注意点
- 对web.config进行新增修改删除读取操作
- 怎样在C#里对xml文件进行修改,新增,删除,插入操作?
- 一个使用GridView显示数据,并且可以进行添加、修改、删除操作的例子
- javascript中模拟hashtable对数组进行快速查找
- 对config文件中的配置进行新增修改删除读取操作
- 数组操作,将数组排序,然后按最大-最小-第二大-第二小。。。。进行
- C#对DataGridView中的数据进行添加、修改、删除操作
- 如何在DataGrid中进行添加、删除和修改操作
- 二分法的应用----------利用随机类生成数组,并用二分法对数组的元素进行查找以及插入操作
- 转载:怎样在C#里对xml文件进行修改,新增,删除,插入操作?
- GridView分页后进行添加,删除操作后,仍返回到当前页码
- 怎样在C#里对xml文件进行修改,新增,删除,插入操作?
- javascript中模拟hashtable对数组进行快速查找
- 巧用标准c++中的算法函数,对数组进行操作
- 使用FSO按文件大小浏览文件目录并进行删除操作
- 利用JDOM及XPath对XML文件进行创建、查找、增加、删除、保存等操作