链表J 不敢死队问题
2017-10-03 19:13
218 查看
Problem Description
有M个敢死队员要炸掉敌人的一个碉堡,谁都不想去,排长决定用轮回数数的办法来决定哪个战士去执行任务。如果前一个战士没完成任务,则要再派一个战士上去。现给每个战士编一个号,大家围坐成一圈,随便从某一个战士开始计数,当数到5时,对应的战士就去执行任务,且此战士不再参加下一轮计数。如果此战士没完成任务,再从下一个战士开始数数,被数到第5时,此战士接着去执行任务。以此类推,直到任务完成为止。
我们假设排长是1号,按照上面介绍,从一号开始数,数到5的那名战士去执行任务,那么排长是第几个去执行任务的? //5的倍数
Input
输入包括多试数据,每行一个整数M(0<=M<=10000)(敢死队人数),若M==0,输入结束,不做处理。
Output
输出一个整数n,代表排长是第n个去执行任务。
Example Input
9
6
223
0
Example Output
2
6
132
有M个敢死队员要炸掉敌人的一个碉堡,谁都不想去,排长决定用轮回数数的办法来决定哪个战士去执行任务。如果前一个战士没完成任务,则要再派一个战士上去。现给每个战士编一个号,大家围坐成一圈,随便从某一个战士开始计数,当数到5时,对应的战士就去执行任务,且此战士不再参加下一轮计数。如果此战士没完成任务,再从下一个战士开始数数,被数到第5时,此战士接着去执行任务。以此类推,直到任务完成为止。
我们假设排长是1号,按照上面介绍,从一号开始数,数到5的那名战士去执行任务,那么排长是第几个去执行任务的? //5的倍数
Input
输入包括多试数据,每行一个整数M(0<=M<=10000)(敢死队人数),若M==0,输入结束,不做处理。
Output
输出一个整数n,代表排长是第n个去执行任务。
Example Input
9
6
223
0
Example Output
2
6
132
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *creat(int n) { struct node *p,*head,*tail; int i; p = (struct node *)malloc(sizeof(struct node)); p->data = 1; p->next = NULL; head = p; tail = p; for(i=2; i<=n; i++) { p = (struct node *)malloc(sizeof(struct node)); p->data = i; p->next = NULL; tail->next = p; tail = p; } tail->next = head; return(head); }; void del(struct node *head) { struct node *p,*q; int num=0,count=0; int num=1;count=0; q = head; q = head; while(q->next!=head) { q = q->next; } while(1) { p = q->next; num++; if(num%5==0) { q->next = p->next; //此处已经将此结点不再参与之后的计数,已经从链表中删除。 count++; if(p->data==1) { printf("%d\n",count); break; } free(p); //删除结点也可以不释放内存空间 } else { q = p; } } } int main() { int m; struct node *head; while(~scanf("%d",&m)&&m!=0) { head = creat(m); del(head); } return 0; }
相关文章推荐
- sdut.acm 2012级《程序设计基础Ⅱ)》_链表 不敢死队问题
- SDUT OJ 不敢死队问题——链表
- 链表之不敢死队问题
- 约瑟夫问题(链表):不敢死队问题
- 【链表】单链表循环解决Josephus问题
- 链表问题---判断一个链表是否为回文结构
- 不敢死队问题
- 链表问题合集
- 链表中环的问题
- 单链表的三个典型问题
- 链表的反转问题(Reverse Linked List)
- 链表相交问题
- 约瑟夫问题(链表经典)
- 数据结构--单链表“环”的问题(1)
- 约瑟夫环问题(丢手帕游戏)Java 链表实现
- 判断链表中是否有环 -- 有关单链表中环的问题
- 敢死队问题
- 与单链表有关的算法问题
- [LeetCode] Reverse Linked List I II - 链表翻转问题
- 判断单链表是否存在环,判断两个链表是否相交问题详解