Counting-out Rhyme<vector实现约瑟夫环模拟>
2017-03-30 17:48
453 查看
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.
代码:
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; typedef long long LL; LL a[100+10]; int main() { LL n,m; vector<int>q; cin>>n>>m; for(int i=1; i<=n; i++) q.push_back(i); int ans=0; for(int i=1; i<=m; i++) { cin>>a[i]; int x=(a[i]%q.size()+ans)%q.size(); cout<<q[x]<<" "; ans=x; q.erase(q.begin()+x); } return 0; }
相关文章推荐
- 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
- [置顶] 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector .
- [置顶] 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
- 对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
- 支持vector,map,list<map>序列化,反序列化实现
- 生产者消费者模型实现<一>模拟实现
- <vector> template实现
- c++ primer(第五版)笔记 第十三章(4) string 类和 vector<string> 容器的简单实现
- 模拟实现通讯录 <一><静态方法>
- C++:模拟实现类似<time.h>的计时功能
- JQuery中模拟点击<a>标签,实现页面跳转
- 用vector<int>对象元素,实现输入10数,将每个奇数值元素用该值的两倍替换
- 约瑟夫循环c语言实现 (<<数据结构与算法>>-王曙燕 约瑟夫环问题解答)
- 模拟实现通讯录<二>(动态模拟)
- 模拟DVD 实现录入 查看 借出 归还 删除 新增 等功能<分层>
- Fresco和 RecyclerView 实现瀑布流 <图片是用本地图片来模拟网络图片的>
- 模拟实现通讯录<三> (文件流)
- <STL>模拟实现Vector
- c#简单实现二维数组和二维数组列表List<>的转置
- 在.Net的WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变<收藏>