Nine degrees 1516 adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.StreamTokenizer; Public classmain{/**     * @paramargs *@throwsIOException*/     Public Static voidMain (string[] args)throwsIOException {//System.setin (New FileInputStream ("data.in"));Streamtokenizer in =NewStreamtokenizer (NewBufferedReader (NewInputStreamReader (system.in));  while(In.nexttoken ()! =streamtokenizer.tt_eof) {            intn = (int) In.nval; intEvenindex = 0, Oddindex = 0; int[] Evenarr =New int[n]; int[] Oddarr =New int[n];  for(inti = 0; I < n; i++) {In.nexttoken (); intm = (int) In.nval; if((M & 1) = = 0) {Evenarr[evenindex++] =m; } Else{Oddarr[oddindex++] =m; }            }             for(inti = 0; i < Oddindex; i++) {                if(I > 0) {System.out.print (" ");            } System.out.print (Oddarr[i]); }            if(Oddindex > 0) {System.out.print (" "); }             for(inti = 0; i < Evenindex; i++) {                if(I > 0) {System.out.print (" ");            } System.out.print (Evenarr[i]);        } System.out.println (); }    }}

Extension: When you don't need to focus on the order of Odd even

 PackageCom.wangzhu.algorithm;Importjava.util.Arrays;ImportJava.util.Scanner; Public classArrayoddevenmove { Public Static voidMain (string[] args) {Scanner cin=NewScanner (system.in);  while(Cin.hasnext ()) {intn =Cin.nextint (); int[] arr =New int[n];  while(Cin.hasnext ()) { for(inti = 0;i < N;i + +) {Arr[i]=Cin.nextint ();                } execute1 (arr);            System.out.println (arrays.tostring (arr)); }        }    }         Public Static voidExecute1 (int[] arr) {        //record the current position of an odd number        intStart =-1;  for(inti = 0, Len = arr.length;i < Len;i + +){            if(!IsEven (Arr[i])) {                //When an odd number is found, the current position is swapped to the consecutive oddStart + +;            Swap (I, start, arr); }        }    }         Public Static voidExecuteint[] arr) {        intleft = 0, right = arr.length-1;  while(Left <Right ) {             while(Left < right &&!)IsEven (Arr[left])) { Left++; }                         while(Left < right &&IsEven (Arr[right])) {                --Right ; }                        if(Left <Right )            {Swap (left, right, arr); }        }    }    Private Static BooleanIsEven (intnum) {        return(num & 1) = = 0; }        Private Static voidSwapintIintJint[] arr) {        intTMP =Arr[i]; Arr[i]=Arr[j]; ARR[J]=tmp; }}

Reference:

Http://www.cnblogs.com/xing901022/p/3755795.html

Http://blog.chinaunix.net/uid-26548237-id-3753247.html

Http://www.cnblogs.com/kaituorensheng/p/3607118.html

Nine degrees 1516 adjust the array order so that the odd digits are preceded by even numbers

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.