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