小孩报数问题
有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。
输入:
第一行输入小孩的人数N(N<=64)
接下来每行输入一个小孩的名字(人名不超过15个字符)
最后一行输入W,S(W<N),用逗号”,”间隔
输出:
按人名输出小孩按顺序出列的顺序,每行输出一个人名
样例输入:
5
Xiaoming
Xiaohua
Xiaowang
Zhangsan
Lisi
2,3
样例输出:
Zhangsan
Xiaohua
Xiaoming
Xiaowang
Lisi
提示:
可用链表来实现
考察重点:约瑟夫算法、链表
参考代码:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_NAME_LEN 16
typedef struct _child {
char name[MAX_NAME_LEN];
struct _child * pre;
struct _child * next;
} Child;
Child* init(Child* init_child) {
if (init_child == NULL) {
return;
}
init_child->pre = init_child;
init_child->next = init_child;
return init_child;
}
void append(Child* list, Child* new_child) {
if (list == NULL || new_child == NULL) {
return;
}
new_child->pre = list->pre;
new_child->next = list;
list->pre->next = new_child;
list->pre = new_child;
}
void select_child(Child* list, int begin, int sel, int count) {
int i;
// selecting begins from the 'sel'th child
for (i=1; i<begin; i++) {
list = list->next;
}
while (count--) {
for (i=1; i<sel; i++) {
list = list->next;
}
printf("%s\n", list->name);
list->pre->next = list->next;
list->next->pre = list->pre;
list = list->next;
}
}
int main()
{
int count;
scanf("%d", &count);
Child* p = (Child*)malloc(sizeof(Child) * count);
memset(p, 0, sizeof(Child) * count);
scanf("%s", p->name);
Child* list = init(p);
int i;
for (i=1; i<count; i++) {
Child* new_child = (Child*)(p+i);
scanf("%s", new_child->name);
append(list, new_child);
}
int begin, sel;
scanf("%d,%d", &begin, &sel);
select_child(list, begin, sel, count);
free(p);
return 0;
} |
“顶嵌杯”----初赛第3题分析
转载上一篇:“顶嵌杯”----初赛第2题分析
下一篇:“顶嵌杯”----初赛第4题分析
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
2011 蓝桥杯【初赛试题】 程序设计题二
公司发了某商店的购物券1000元,限定只能购买店中的m种商品。每种商品的价格分别为m1,m2,…,要求程序列出所有的正购买的数量,中间用空格分隔
2011 蓝桥杯 初赛试题 程序设计题 C语言 ACM C++ OJ i++ #include 数组 -
2011蓝桥杯【初赛试题】程序设计题三
一种Playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重复,且都为小写字母),然后与字母表中其他字母一起填入至一个5x5的方阵中,填入方法如下:1.首先按行填入密钥串。2.紧接其后,按字母序按行填入不在密钥串中的字母。3.由于方阵中只有25个位置,最后剩下的那个字母则不需变换。如果密钥
2011蓝桥杯 初赛试题 程序设计题三 C语言 ACM C++ OJ i++ 字符串 #include -
2011蓝桥杯【初赛试题】程序设计题一
方阵的主对角线之上称为“上三角”。请你设计一个用于填充n阶方阵的上三角区域的程序。填充的规则是:使用1,2,3….的自然数列,从左上角开始,按照顺时针方向螺旋填充。例如:当n=3时,输出:1 2 36 45当n=4时,输出:1 2 3 49 10 58 67当n=5时,输出:1 2 3 4 512 13 14 611 15 710 8
2011蓝桥杯 初赛试题 程序设计题一 C语言 ACM C++ OJ i++ #include 顺时针 -
2013蓝桥杯【初赛试题】第39阶台阶
第39阶台阶小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级! 站在台阶前,他突然又想着一个问题: 如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢? 请你利用计算机的优势,帮助小明寻找交解答过程
2013蓝桥杯 初赛试题 第39阶台阶 C语言 ACM C++ OJ ide 递归 斐波那契