Soldier Queue Problem
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 The main topic: Soldiers queued to count, first 1 to 2 count, report 2 of the row, 1 to 3 count off, report 3 of the row, after each of the remainder to the small serial number, until the remaining people not more than 3 people so far. Enter the number of recruits and output the original number of the remaining recruits. Analysis: Use A to record the remaining people after each count, until a<=3. Code:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 intMain ()6 {7 intd[5001];8 intn,t,i,a,b;9scanf"%d",&N);Ten while(n--) One { Ascanf"%d",&T); -A=T; - for(i=1; i<=t;i++) thed[i]=i; - while(a>3) - { -b=0; + for(i=1; i<=t;i++) - { + if(d[i]==0)Continue; A Elseb+=1; at if(b==2) - { -d[i]=0; -b=0; - } - } ina=a-a/2; - if(a<=3) Break; tob=0; + for(i=1; i<=t;i++) - { the if(d[i]==0)Continue; * Elseb+=1; $ if(b==3)Panax Notoginseng { -d[i]=0; theb=0; + } A } thea=a-a/3; + } -b=0; $ for(i=1; i<=t;i++) $ { - if(d[i]==0)Continue; - Else the { -b+=1;Wuyi if(b==a) theprintf"%d\n", D[i]); - Else Wuprintf"%d", D[i]); - } About } $ } - return 0; -}
Experience:
The problem is not very difficult, so it's a little faster. I need to do some more of these problems to exercise myself, and then find some more of these problems to do. Come on!
Soldiers lined up