Saturday, October 30, 2010

poj_3750_小孩报数问题.cpp

Problem Links:


poj3750,

Problem:


小孩报数问题















Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4457Accepted: 2145


Description
有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。

Input
第一行输入小孩的人数N(N<=64)
接下来每行输入一个小孩的名字(人名不超过15个字符)
最后一行输入W,S (W < N),用逗号","间隔

Output
按人名输出小孩按顺序出列的顺序,每行输出一个人名

Sample Input
5
Xiaoming
Xiaohua
Xiaowang
Zhangsan
Lisi
2,3

Sample Output
Zhangsan
Xiaohua
Xiaoming
Xiaowang
Lisi

Source

Solution:


Simulation.

Source Code:


[sourcecode language="cpp" collapse="true" padlinenumbers="true"]
//Sat 30 Jan 2010 05:48:22 PM CST
#include <iostream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
vector<string> v;
v.clear();
int N;
string str;
cin >> N;
for(int i=0; i<N; i++)
{
cin >> str;
v.push_back(str);
str.clear();
}
int start;
int loop;
char c;
cin >> start >> c >> loop;
// cout << start << ", " <<loop << endl;
start--;
while(!v.empty())
{
int next = (start+loop-1) % v.size();
cout << v[next] << endl;
start = next==v.size()-1 ? 0 : next;
v.erase(v.begin() + next);
}
return 0;
}
[/sourcecode]

No comments :