码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1276:士兵队列训练问题(纯C)

时间:2015-07-21 22:00:15      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

Description

         某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。        
        

Input

本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。        
        

Output

共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。        
        

Sample Input

2
20
40
                

Sample Output

1 7 19
1 19 37
 
本题思路:
  
  用一个一维数组构成一个长数列。
  第一遍喊一二时,喊一的排到队尾去,喊二的直接跳过,head一直往前移动,直到到达开始报数时tail所在的位置。
  第二遍喊一二三时,喊一二的排到队尾,喊三的直接跳过,head的移动同上。
  循环往复,直到head和tail之间只剩下不超过三个人为止。
  然后把剩余士兵的编号打印出来。
 
注意:
  
  最开始队伍中就不超过三个人的情况也需要注意。
 
  
 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int a[15000],i,k,n,t,head,tail,count;
 6 
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         for(k=0;k<n;k++)
10         {
11             scanf("%d",&t);
12             if(t==1)
13                 {printf("1\n");continue;}
14             else if(t==2)
15                 {printf("1 2\n");continue;}
16             else if(t==3)
17                 {printf("1 2 3\n");continue;}
18 
19             for(i=0;i<=t;i++)
20                 a[i]=i;
21             head = 1;
22             tail = t+1;
23             while(tail-head>3)
24             {
25                 count = tail-1;
26                 while(head<=count)
27                 {
28                     a[tail]=a[head];
29                     tail++;
30                     head+=2;
31                 }
32                 head = count+1;
33                 if(tail-head>3)
34                 {
35                     count = tail-1;
36                     while(head<=count)
37                     {
38                         a[tail]=a[head];
39                         tail++;
40                         head++;
41                         if(head<=count)
42                         {
43                             a[tail]=a[head];
44                             tail++;
45                             head+=2;
46                         }
47                     }
48                     head = count+1;
49                     continue;
50                 }
51             }
52             count = tail - head;
53             for(i=0;i<count;i++)
54             {
55                 if(i!=count-1)
56                     printf("%d ",a[head+i]);
57                 else
58                     printf("%d\n",a[head+i]);
59             }
60         }
61     }
62     return 0;
63 }

 

HDU 1276:士兵队列训练问题(纯C)

标签:

原文地址:http://www.cnblogs.com/yoghurtman/p/4665538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!