poj 3750 小孩报数问题

小孩报数问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11527   Accepted: 5293

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <queue>
 5 #include <stack>
 6 #include <iostream>
 7 using namespace std;
 8 struct node{
 9     string name;
10     node *next;
11 };
12 int main(){
13     //freopen("D:\INPUT.txt","r",stdin);
14     int n;
15     scanf("%d",&n);//用例只有一组,写成while(scanf("%d",&n)),会超时
16     int i,j;
17     node *p,*head,*tail;
18     head=tail=new node();
19     cin>>(head->name);
20     head->next=head;
21     for(i=1;i<n;i++){
22         p=new node();
23         cin>>p->name;
24         p->next=head;//循环链表
25         tail->next=p;
26         tail=p;
27     }
28     int w,s;
29     scanf("%d,%d",&w,&s);
30     w--;
31     while(n>1){
32         w=(w+s-1)%n;//下一个编号
33         node *q=tail;
34         p=head;
35         for(i=1;i<=w;i++){
36             q=p;
37             p=p->next;
38         }
39         cout<<p->name<<endl;
40         q->next=p->next;
41         delete p;
42         if(!w){
43             head=q->next;
44         }
45         else{
46             if(w==n-1){
47                 tail=q;
48             }
49         }
50         n--;
51     }
52     cout<<head->name<<endl;
53     delete head;
54     return 0;
55 }
原文地址:https://www.cnblogs.com/Deribs4/p/4644460.html