Description a troop to train the recruits, the recruits from the beginning of the sequence numbered sequentially, ranks, the rules of the training are as follows: Start from the beginning of one to two off, where the second column, the remaining to the small number direction, and then start from the beginning of one to three off, where the report of three, The rest to the small number direction, continue to start from scratch from one to two off ... , starting from the beginning of the rotation of one to two off, one to three off until the remaining number of people not more than three.
Input is a number of test data groups, the first behavior group N, followed by the number of N recruits, the number of recruits not more than 5000.
Output total n rows, corresponding to the number of recruits, each output of the remaining recruits the original number, there is a space between the numbers.
Sample Input
2 20 40
Sample Output
1 7 19 1 19 37: N Soldiers in accordance with the order of 1~n, first from left to right, 1~2 alternating, report 2 out of the team, and then from left to right 1~3 alternately numbered, report 3 out of the team, the cycle of these two processes, until the number of queues <=3. The initial number of the remaining person is output. Train of thought: direct simulation of two processes, until the number of <=3, the final output results. Code:
#include <cstdio> int a[5001];
using namespace Std;
int main () {int n,x,b,c;
scanf ("%d", &n);
while (n--) {scanf ("%d", &x);
B=x;
for (int i=1;i<=x;i++) a[i]=i;
while (b>3) {c=0;
for (int i=1;i<=x;i++) {if (a[i]==0) continue;
else c+=1;
if (c==2) {a[i]=0;
c=0;
}} B=B-B/2;
if (b<=3) break;
c=0;
for (int i=1;i<=x;i++) {if (a[i]==0) continue;
else c+=1;
if (c==3) {a[i]=0;
c=0;
}} B=B-B/3;
} c=0;
for (int i=1;i<=x;i++) {if (a[i]==0) continue; else {c+=1;
if (c==b) printf ("%d\n", A[i]);
else printf ("%d", a[i]);
}} return 0;
}