hdu1276 (soldier queue Training problem) Java collection water over

Source: Internet
Author: User

Click to open link

Some people say that this problem belongs to the stack or queue, personally think that the collection should be more accurate points.

Problem description A troop to train the recruits, numbering the recruits sequentially from the start. A line ranks, the rules for training such as the following: From the beginning to two off, where reporting two out. The rest of the small ordinal direction, and then start from the beginning of one to three count. 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 from start to finish two, one to three count off until the remaining number of not more than three people.


Input has multiple test data sets. Number of first groups N, followed by n rows of recruits, the number of recruits does not exceed 5000.

Output co-owns n rows, the number of recruits entered accordingly, 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

Attention:

Here to pay attention to the title of the one or two. Refers to all people in order after the completion of the report, and then 123. If you have:

First time off one or two: Staff: 1 2 3 4 5 6 7 8 9 10

Count off: 1 2 1 2 1 2 1 2 1 2

Second off 123: Staff: 1 3 5 7 9

Count off: 1 2 3 1 2

So loop the above two steps. Know the number of persons not exceeding three.

Another detail is in the output is, control the formatting problem, the last one cannot have a space behind

Code:
Import Java.util.iterator;import Java.util.linkedlist;import Java.util.scanner;public class P1276 {public static void Main (string[] args) {Scanner sc=new Scanner (system.in); int n=sc.nextint (); int num; Linkedlist<integer> List;while (n-->0) {num=sc.nextint (); list=new linkedlist<integer> (); for (int i=0; i<num;i++) {//Add person List.add (i+1) to the collection;} Iterator<integer> it=queue.iterator ();//while (It.hasnext ()) {//system.out.print (It.next () + "");//}boolean Flag=true;while (List.size () >3) {//system.out.println (List.size ()) and if (flag) {//control between one or two and 123 modes for each other for (int i=1;i <list.size (); i+=1) {//system.out.print (List.get (i) + "); List.remove (i);//Remove the person shouting to two flag=false;} System.out.println ();} else{for (int i=2;i<list.size (); i+=2) {//system.out.print (List.get (i) + "); List.remove (i);//Remove the person shouting to three flag=true;} System.out.println ();}} int remainnum=list.size ();//must record the number of results in advance iterator<integer> it=list.iterator (); int count=0;// Used to control the last space problem while (It.hasnext ()) {count++;if (count==remainnum) {//cannot be used here list.siZe (), as the output is, the corresponding element has gone out. All size changed System.out.println (It.next ());} Else{system.out.print (It.next () + "");}}}}




hdu1276 (soldier queue Training problem) Java collection water over

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.