Queue, java queue
Description
A certain army trains new recruits in the queue. The new recruits are numbered in sequence from the beginning and lined up in a row. The training rules are as follows: report the number one to two from the beginning, and report the number to the second column, the rest will move closer to the Small Serial Number direction, and then start from scratch to report one to three. If the three columns are reported, the rest will move closer to the Small Serial Number direction, continue to start from scratch for one to two reports ..., one to two reports and one to three reports from the beginning until the remaining number of people does not exceed three.
Input
There are multiple test data groups in this question. The first behavior group is N, followed by N rows of new recruits. The number of new recruits cannot exceed 5000.
Output
There are N rows, which correspond to the number of new recruits. Each row outputs the initial number of new recruits, with a space between the numbers.
Sample Input
22040
Sample Output
1 7 191 19 37 solutions (in fact, they only come out after reading others' blogs): There are two scenarios: n> 3 and n <= 3. If it is the first type, the output is good. if it is the second type, the two if values in the loop are marked with flag.
The Code is as follows:
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
Int T;
Int a [5005];
Cin> T;
While (T --)
{
Int n, I;
Cin> n;
For (I = 0; I <n; I ++)
A [I] = I + 1;
Int sum = n;
If (n <= 3)
{
If (n = 1)
Cout <1 <endl;
If (n = 2)
Cout <1 <"" <2 <endl;
If (n = 3)
Cout <1 <"" <2 <"" <3 <endl;
Continue; // end this group of cases directly
}
While (1)
{
Int flag = 0;
For (I = 0; I <n; I ++)
{
If (a [I]) // judge that a [I] is not zero
Flag ++; // count
If (flag = 2) // if it is equal to 2, the number of records with 2 is changed to 0.
{
Flag = 0;
A [I] = 0;
Sum --; // indicates the number of remaining persons and the number of non-zero persons.
}
}
If (sum <= 3)
Break;
Flag = 0;
For (I = 0; I <n; I ++)
{
If (a [I])
Flag ++;
If (flag = 3)
{
Flag = 0;
A [I] = 0;
Sum --;
}
}
If (sum <= 3)
Break;
Flag = 0;
}
Cout <1;
Sum --;
For (I = 1; I ++)
{
If (a [I])
{
Cout <"" <a [I];
Sum --;
}
If (! Sum)
Break;
}
Cout <endl;
}
Return 0;
}