hdu-1276 士兵队列训练问题

题目链接;

http://acm.hdu.edu.cn/showproblem.php?pid=1276

题目类型:

数据结构-链表、模拟

题意概括:

先1、2、1、2的报数,报到2的出列,在1、2、3、1、2、3的报数,输出剩下三个人的时候,他们最初的编号。

解题思路:

通过链表模拟过程。

题目:

士兵队列训练问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8530    Accepted Submission(s): 3797


Problem Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
 
Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
 
Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
 
Sample Input
2
20
40
 
Sample Output
1 7 19
1 19 37
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct student {
    int data;//数据域   (存数值) 
    struct student *next;//指针域 
};
int le(struct student *head)//求链表长度 
{
    struct student *p;
    int k=0;
    p=head;
    while(p)
    {
        k++;
        p=p->next;// 
    }
    return k;
}
int main()
{
    int t,n,i,o;
    while(scanf("%d",&t)!=EOF)
    {
        struct student *head,*p,*q,*tail;// P是当前,q是下一个 
        while(t--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)//赋值
            {
                p=(struct student *)malloc(sizeof(struct student));
                if(i==1)
                head=p;
                else
                tail->next=p;
                p->data=i;
                //scanf("%d",p->data);
                p->next=NULL;
                tail=p;
             } 
             o=n;
             p=head;
             if(o<=3)
             {
                 printf("%d",p->data);//数值在DATA 里面储存 
                 p=p->next;
                 while(p)
                 {
                     printf(" %d",p->data);
                     p=p->next;
                 }
                 printf("
");
             }
             else
             {
                 int k,r=n;
                 int ret=3;
                 while(1)
                 {
                     k=1;
                     p=head;
                     if(ret==2)
                             ret=3;
                         else 
                             ret=2;
                     while(p->next!=NULL)
                     {
                         
                         k++;
                         if(k%ret==0)//删除报数是2的 
                         {
                             q=p->next;
                             p->next=q->next;
                             free(q);//释放空间 
                             r--;
                         }
                         else
                         p=p->next; 
                     }
                     if(r<=3)
                     break;
                 }
                 p=head;
                 printf("%d",p->data);//数值在DATA 里面储存 
                 p=p->next;
                 while(p)
                 {
                     printf(" %d",p->data);
                     p=p->next;
                 }
                 printf("
");
             }
        }
    }
}
原文地址:https://www.cnblogs.com/love-sherry/p/6942529.html