士兵排列

 1 #include <iostream>
 2     using namespace std;
 3     int main()
 4     {
 5         int T, n;
 6         cin >> T;
 7         while (T--)
 8         {
 9             cin >> n;
10             if (n <= 3)
11             {
12                 if (n == 1)
13                     cout << "1" << endl;
14                 if (n == 2)
15                     cout << "1 " << "2" << endl;
16                 if (n == 3)
17                     cout << "1 " << "2 " << "3" << endl;
18             }
19             else
20             {
21                 int a[5000];
22                 for (int k = 0; k <= n; k++)
23                     a[k] = 1;
24                 int j = 0, b = 2, m = n;
25                 while (m > 3)
26                 {
27                     j = 0;
28                     for (int i = 1; i <= n; i++)
29                         if (a[i] == 1)
30                         {
31                             j++;
32                             if (j == b)
33                             {
34                                 a[i] = 0;
35                                 j = 0;
36                             }
37                         }
38                     m -= (m / b);
39                     if (b == 2) b = 3;
40                     else b = 2;
41                 }
42                 int c = 0;
43                 for (int i = 1; i <= n; i++)
44                 {
45                     if (a[i] == 1)
46                     {
47                         c++;
48                         cout << i;
49                         if (c < m)
50                             cout << " ";
51                         else
52                             cout << endl;
53                     }
54                 }
55             }
56         }
57     }

原文地址:https://www.cnblogs.com/edych/p/7198666.html