小孩报数问题
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9258 | Accepted: 4338 |
Description
有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。
Input
第一行输入小孩的人数N(N<=64)
接下来每行输入一个小孩的名字(人名不超过15个字符)
最后一行输入W,S (W < N),用逗号","间隔
接下来每行输入一个小孩的名字(人名不超过15个字符)
最后一行输入W,S (W < N),用逗号","间隔
Output
按人名输出小孩按顺序出列的顺序,每行输出一个人名
Sample Input
5 Xiaoming Xiaohua Xiaowang Zhangsan Lisi 2,3
Sample Output
Zhangsan Xiaohua Xiaoming Xiaowang Lisi
练习了一下双向链表,虽然这个用不上双向的。。。题很水,直接模拟链表操作就可以了。。。
#include <iostream> #include<algorithm> #include <stdio.h> #include <string.h> #include <math.h> #define MAX_NUM 11111 using namespace std; struct LIST{ char name[22]; struct LIST *f; struct LIST *r; }; int main() { int w, s, n; int i; freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); scanf("%d", &n); // printf("n = %d\n", n); struct LIST *node, *head; head = node = new LIST; for (i = 0;i < n - 1;i ++){ scanf("%s", node->name); // printf("%s\n", node->name); node->r = new LIST; node->r->f = node; node = node->r; } scanf("%s", node->name); node->r = head; node->r->f = node; scanf("%d,%d", &w, &s); // printf("w = %d, s = %d\n", w, s); node = head;w--; while(w--){ node = node->r; } while(n--){ for (i = 1;i < s;i ++){ node = node->r; // printf("%d = test = %s\n", i, node->name); } printf("%s\n", node->name); node->f->r = node->r; node->r->f = node->f; node = node->r; // printf("test= %s\n", node->name); } return 0; }