sdut oj2056 不敢死队问题

题目链接:点击打开链接

不敢死队问题

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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

 

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

 

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

输入

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

 

输出

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

示例输入

962230

示例输出

26132

提示


 

代码实现:

#include 
#include 
#include 
using namespace std;

struct node
{
    int data;
    node *next;
};

node *create(int n)
{
    node *head,*tail,*p;
    head=new node;
    head->next=NULL;
    tail=head;
    p=new node;
    p->data=1;
    p->next=NULL;
    head=p;
    tail=p;
    for(int i=2;i<=n;i++)
    {
        p=new node;
        p->data=i;
        tail->next=p;
        p->next=NULL;
        tail=p;
    }
    tail->next=head;
    return head;
}

void Del(node *head,int n)
{
    int num=0;///
    int Count=0;///出圈人数
    node *p,*q;
    p=head->next;
    while(p->next!=head)
        p=p->next;
    while(Countnext;
        num++;
        if(num%5==0)
        {
            if(q->data==1)
            {
                Count++;
                printf("%d\n",Count);
                break;
            }
            else
            {
                p->next=q->next;
                Count++;
            }
        }
        else
            p=p->next;
    }
}

int main()
{
    int n;
    node *head;
    while(~scanf("%d",&n)&&n)
    {
        head=create(n);
        Del(head,n);
    }
    return 0;
}


 

你可能感兴趣的:(链表)