士兵队列训练问题

题目:

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

Input本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。 
Output共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。 
Sample Input

2
20
40

Sample Output

1 7 19
1 19 37

解法一:我的解法
比较简单的题目,没有太多技巧
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 #define MAX 5005
 6 
 7 int main()
 8 {
 9     int N;
10     cin>>N;
11 
12     for(int i1 = 1;i1<=N;i1++)
13     {
14         int a[MAX],b[MAX];
15         int temp,temp1,temp0;                  //这里使用了三个temp,temp和temp0是用来表示剩余人数的,temp1是进行输出判定的
16         cin>>temp;
17         temp0 = temp;
18         for(int i = 1;i<=temp;i++)
19             a[i] = i;
20 
21         if(temp<=3)
22         {
23              for(int i = 1;i<=temp;i++)
24                 if(i == 1)
25                     cout<<a[i];
26                 else
27                     cout<<' '<<a[i];
28                 cout<<endl;
29         }
30         else
31         {
32                 while(1)
33         {
34             for(int i = 1,j = 1;i <= temp ;i++)
35                 if(i%2 == 0)
36                 {
37                     a[i] = 0; temp0--;
38                 }
39                 else
40                 {
41                     b[j++] = a[i];
42                 }
43             temp = temp0;
44 
45             if(temp<=3) { temp1 = 2; break; }
46 
47             for(int i = 1,j = 1;i <= temp ;i++)
48                 if(i%3 == 0)
49                 {
50                     b[i] = 0; temp0--;
51                 }
52                 else
53                 {
54                     a[j++] = b[i];
55                 }
56             temp = temp0;
57 
58             if(temp<=3) { temp1 = 1; break; }
59         }
60 
61         if(temp1 == 1)
62             {
63             for(int i = 1;i<=temp;i++)
64                 if(i == 1)
65                     cout<<a[i];
66                 else
67                     cout<<' '<<a[i];
68                     cout<<endl;
69             }
70         else
71         {
72             for(int i = 1;i<=temp;i++)
73                 if(i == 1)
74                     cout<<b[i];
75                 else
76                     cout<<' '<<b[i];
77                 cout<<endl;
78         }
79 
80         }
81 
82     }
83 
84     return 0;
85 }
原文地址:https://www.cnblogs.com/a2985812043/p/7194749.html