Description
A troop of recruits queue training, the recruits from the beginning in order sequentially numbered, side-by-line ranks, the training rules are as follows: From the beginning of a two count, where the two check out, the remaining to the small ordinal direction, and then from the beginning of a three count, where reporting three of the row, the remaining to the small ordinal direction, Continue from scratch to two count off ... , from beginning to end in rotation from one to two, one to three count off until the remaining number of not more than three people.
Input
There are multiple test data sets, number of first action Group N, followed by n rows of recruits, the number of recruits not exceeding 5000.
Output
Total n rows, corresponding to the number of recruits entered, each line outputs the original number of the remaining recruits, with a space between the numbers.
Sample Input
2 20 40
Sample Output
1 7 19 1 19 37 Analysis: With an array to save it, the first will be labeled as an even number of the save, subscript for the odd number of out, the second will be i%3==2, and then press the first, two times this cycle, when the number of less than, equal to 3 o'clock, the output is a number of elimination.
#include <iostream>
#include <cstring>
#include <list>
Const int maxn=5000+5;
using namespace Std;
Int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
int n, A[MAXN];
Memset (A, 1, sizeof (a));
scanf ("%d", &n);
for (int i = 1; I <= n; i++)
A[i] = i;
int k = n;
int loge = 2;
while (K > 3)
{
int q = 0;
if (Loge = = 2)
{
for (int i = 1; I <= n; i++)
if (a[i]! = 0 && ++q = = 2)
{
Q = 0;
A[i] = 0;
k--;
}
Loge = 3;
Else
{
for (int i = 1; I <= n; i++)
if (a[i]! = 0)
{
if (a[i]! = 0 && ++q = = 3)
{
Q = 0;
A[i] = 0;
k--;
}
}
Loge = 2;
}
}
Loge = 1;
for (int i = 1; I <= n; i++)
if (a[i]! = 0)
if (Loge)
{
cout << i;
Loge = 0;
}
Else
cout << ' << i;
cout << Endl;
}
return 0;
}
Problem C Linked list