您的位置:首页 > 其它

不敢死队问题

2017-03-06 07:02 211 查看
点击获取原题 链接

不敢死队问题
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

说到“敢死队”,大家不要以为我来介绍电影了,因为数据结构里真有这么道程序设计题目,原题如下:

有M个敢死队员要炸掉敌人的一个碉堡,谁都不想去,排长决定用轮回数数的办法来决定哪个战士去执行任务。如果前一个战士没完成任务,则要再派一个战士上去。现给每个战士编一个号,大家围坐成一圈,随便从某一个战士开始计数,当数到5时,对应的战士就去执行任务,且此战士不再参加下一轮计数。如果此战士没完成任务,再从下一个战士开始数数,被数到第5时,此战士接着去执行任务。以此类推,直到任务完成为止。

这题本来就叫“敢死队”。“谁都不想去”,就这一句我觉得这个问题也只能叫“不敢死队问题”。今天大家就要完成这道不敢死队问题。我们假设排长是1号,按照上面介绍,从一号开始数,数到5的那名战士去执行任务,那么排长是第几个去执行任务的?
Input

输入包括多试数据,每行一个整数M(0<=M<=10000)(敢死队人数),若M==0,输入结束,不做处理。

Output

输出一个整数n,代表排长是第n个去执行任务。
Example Input

9
6
223
0

Example Output

2
6
132

Hint

Author

SDUTACM运维技术中心
Copyright © 2013-2014 SDUTACM Team. All Rights Reserved.
2017/3/6 上午6:59:45


#include <bits/stdc++.h>
using namespace std;
struct node ///循环链表的节点
{
int id;
struct node *next;
};
struct node *creat(int n)///建立循环链表
{
struct node *head=new node ,*p,*tail;
head->next=NULL;
tail=head;
for(int i=1;i<=n;i++)
{
p=new node ;
p->id=i;
tail->next=p;
tail=p;
}
tail->next=head->next;///循环起来
free(head);
return tail->next;///返回第一个节点

};
int Dell(struct node *head)///找到排长什么时候上战场
{
int count=0;
node *p=head;///游动指针
while(1)
{
if(p->id)///还未被派遣
{
count++;
if(coun
4000
t%5==0)///数到五
{
if(p->id==1)
{
return count/5;/// 到排长的次数
}
else p->id=0;
p=p->next;
}
else p=p->next;///继续数数
}
else p=p->next;
}
}

int main()
{
int n;
while(cin>>n && n)///N个人
{
struct node *head=creat(n);///建立循环链表
int t=Dell(head);///逐个派遣士兵知道排长上战场
cout<<t<<endl;
}
}

/***************************************************
User name: rj
Result: Accepted
Take time: 0ms
Take Memory: 508KB
Submit time: 2017-03-06 06:58:05
****************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: