Soldier Queue Training ProblemTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4656 Accepted Submission (s): 2175
Problem description A troop to carry out the Recruit queue training, the recruits from the beginning in order sequentially numbered, row by line ranks, the rules of training are as follows: From the beginning to two count, where the two check out, the remaining to the small ordinal direction, and then from the beginning of one to three off, Where report three of the row, the rest of the direction of the small number, continue to start from the beginning of one 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 has a number of test data sets, number of first action Group N, followed by n rows of recruits, the number of recruits not more than 5000.
Output has 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
22040
Sample Output
1 7 191) 19 37
problem-solving ideas: In fact, this is a water can not water the topic, with two array alternating value, according to test instructions use array subscript to remove the removal. But bloggers use Javaarrylist, after all, arrylist is not my own write so use up always check API, and then various problems, because arrylist in the list.remove (int index) function is to remove the subscript element, and the trend to change the list, so use must pay attention to these details, or really waste time.
Code implementation:
Import Java.util.arraylist;import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner SC = new Scanner (system.in); arraylist<integer> list = new arraylist<integer> (); Using ArrayList to implement queueing int n = sc.nextint (); Receive the following set of test data while (n--> 0) {int count = Sc.nextint ();//Receive the number of people queued//assigned, put everyone's numbers add () to the list of lists for (int i = 1; I <= Count i++) {list.add (i);} Boolean flag = true; Use the markup to implement the alternate while (List.size () > 3) {if (flag) {()} (int i = 1; i < list.size (); i++) {list.remove (i);//Direct removal is 2 Number of multiples}flag = false;} else {for (int j = 2; J < List.size (); j + = 2) {list.remove (j);//Direct removal is a multiple of 3}flag = True;}} System.out.print (list.remove (0));//outputs the first value while (!list.isempty ()) {//each time the first value is taken out until the list is empty System.out.print ("" + List.remove (0));} System.out.println (); }}}
Results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1276 Java Fruit