Constructing queue problems (cyclic queue morphing Java) __ Loop queue

Source: Internet
Author: User
Tags ming

1. The original question of cattle net:

topic Description Xiao Ming students put 1 to n these n numbers in a certain order into a queue Q. Now he has executed the following procedure for queue Q:

while (! Q.empty ())              //Queue not empty, execute loop

{

    int x=q.front ();            Remove the value of the current team head x

    q.pop ();                 Eject current team head

    Q.push (x);               Put X into team tail

    x = Q.front ();              Take out this time the value of the team head

    printf ("%d\n", x);          Output x

    q.pop ();                 Pop up this time the team head

}

Do not eject the current team head when the value of the team header is removed.
Xiao Ming students found that this program is exactly in order to output the 1,2,3,..., N. Now Xiaoming wants you to construct the original queue, can you do it. [Note: The original title sample third line 5 error, should be 3, the following has been amended] Enter a description:
The first line, an integer T (t≤100), represents the number of data sets, each of which enters a number of n (1≤n≤100000), and no more than 200000 of all n is entered.
Output Description:
For each set of data, the output line represents the original queue. The numbers are separated by a space and do not output extra spaces at the end of the line.
Example 1 input
4
1
2
3
10
Output
1
2 1
2 1 3 8 1 6 2 10 3 7 4-9 5


2, code: Already a

Package schooloffer17;

Import Java.util.Scanner;
 /** * Created by Caoxiaohong on 17/11/5 13:27. * Xiaoming students put 1 to n these n numbers according to a certain degree of shun ... * < construct queue ></construction Queue >/public class Buildqueue {public static void main (string[] A
        RGS) {Scanner scanner=new Scanner (system.in);
        int T=scanner.nextint ();
        int n;
            while (t-->0) {n=scanner.nextint ();
            Int[] Numbers=new Int[n];
            int num=1;//Inserts a number Boolean isfind=false;//identifies the current pop-up element before it finds an element that is inserted into the tail of the team and can only be popped if found.
                    for (int i=0;num<=n;i%=n) {if (Isfind==false && numbers[i]==0) {isfind=true;
                i++;
                }else if (isfind==false && numbers[i]!=0) {i++;
                }else if (isfind==true && numbers[i]!=0) {i++;
                    }else if (isfind==true && numbers[i]==0) {numbers[i]=num++;
Isfind=false;                    i++; //output for (int i=0;i<n;i++) {if (i<n-1) Sys
                Tem.out.print (numbers[i]+ "");
            else System.out.println (Numbers[i]);
 }
        }
    }
}



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.