Idea: The elements of array A are 1,2,3,3; dictionary ordering is to find the next larger than 1,2,3,3 array sequence, namely 1,3,2,3;
The steps are as follows: 1. First use Arrays.sort () to sort the sorted array, such as input 3213, order 1233, from the last element of the array (i.e. I = array length), compare a[i] and a[i-1], and find the first a[i-1]<a[i] The i-1;
2, at this time, A[i] may also exist in front of the element larger than A[i-1], starting from the end of the array to find the first more than a[i-1] large element x, will x and A[i-1] Exchange, to Arrays.sort (A,i,a.length);
1 Public classSolution {2 Public Static voidMain (String args[]) {3Scanner scan =NewScanner (system.in);4 intn =scan.nextint ();5 int[] A =New int[n];6 intI,j = 0;7 inttemp = 0;8 for(i = 0; i<n; i++)9A[i] =scan.nextint ();Ten Arrays.sort (a); One for(i = 0; i<a.length;i++){ ASystem.out.print (A[i] + ""); - } - System.out.println (); the while(true){ - for(i = a.length-2; i>=0; i--){ - if(A[i+1] >A[i]) { - //i = i-1; + Break; - } + } A if(I < 0) at Break; - - for(j = a.length-1; j> i; j--){ - if(A[j] >A[i]) { - intK =A[i]; -A[i] =A[j]; inA[J] =K; - Break; to } + } -Arrays.sort (a,i+1, a.length); the for(i = 0; i<a.length;i++){ *System.out.print (A[i] + ""); $ }Panax Notoginseng System.out.println (); - } the + } A}
Dictionary Ordering-Perfection arrangement (elements have duplicates)