任意排序的10个整数(0~100),彼此以空格分隔。
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。 1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现; 2. 输入数据随机,有可能相等。
4 7 3 13 11 12 0 47 34 98
47 13 11 7 3 0 4 12 34 98
import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { // TODO Auto-generated method stub // 问题:输入10个数,奇数降序,偶数升序 // 样例输入: 1 2 3 4 5 6 7 8 9 0 // 4 7 3 13 11 12 0 47 34 98 // 样例输出: 9 7 5 3 1 0 2 4 6 8 // 47 13 11 7 3 0 4 12 34 98 Integer[] arrs = new Integer[10]; Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int count = 0; // 计数:偶数个数 for (int i = 0; i < arrs.length; i++) { arrs[i] = sc.nextInt(); if (arrs[i] % 2 == 0) { count++; } } // 找出每个偶数存入s1,剩下的存入s2 int x = 0, y = 0; Integer[] s1 = new Integer[count]; Integer[] s2 = new Integer[10 - count]; for (int i = 0; i < arrs.length; i++) { if (arrs[i] % 2 == 0) { s1[x++] = arrs[i]; } else { s2[y++] = arrs[i]; } } // 排序 Arrays.sort(s1); //偶数升序 Arrays.sort(s2,Collections.reverseOrder()); //奇数降序 // foreach,注意顺序,先寄后偶 for(int i : s2) { System.out.print(i+" "); //注意⚠️此处为 i } for(int i : s1) { System.out.print(i+" "); } } } }
import java.util.*; public class Main implements Comparable<Main>{ int val; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { Main a[]=new Main[10]; for(int i=0;i<10;i++) { Main m=new Main(); m.val=sc.nextInt(); a[i]=m; } Arrays.sort(a); for(Main i:a) { System.out.print(i.val+" "); } } } @Override public int compareTo(Main o) { if(o.val%2==0&&this.val%2==0)return this.val-o.val; else if(o.val%2==1&&this.val%2==1)return o.val-this.val; else if(o.val%2==1)return 1;//决定奇数在前还是偶数在前(试出来的) else return -1; } } //快排:让我写的有点乱,其实不写快排,直接调用Collections.sort()更方便 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int index=0; List<Integer>list1=new ArrayList<>(); List<Integer>list2=new ArrayList<>(); while(index++<10) { int n=sc.nextInt(); if(n%2==0)list2.add(n); else list1.add(n); } int []a1=new int[list1.size()]; int []a2=new int[list2.size()]; for(int i=0;i<a1.length;i++) { a1[i]=list1.get(i); } for(int i=0;i<a2.length;i++) { a2[i]=list2.get(i); } sort(a1,0,list1.size()-1); sort(a2,0,list2.size()-1); for(int i=a1.length-1;i>=0;i--) { System.out.print(a1[i]+" "); } for(int i:a2) System.out.print(i+" "); System.out.println(); } } private static void sort(int[] number, int l, int r) { if (l < r) { int i = l; int j = r; int x = number[i]; while (i < j) { while (i < j && number[j] >= x) j--; if (i < j) { number[i] = number[j]; i++; } while (i < j && number[i] < x) { i++; } if (i < j) { number[j] = number[i]; j--; } } number[i] = x; sort(number, l, i - 1); sort(number, i + 1, r); } } }
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ ArrayList<Integer> even = new ArrayList<>(); ArrayList<Integer> odd = new ArrayList<>(); for (int i = 0; i < 10; i++) { int n = scanner.nextInt(); if (n%2==0) even.add(n); else odd.add(n); } Collections.sort(even); Collections.sort(odd); // odd 降序 for (int i = odd.size()-1; i >=0 ; i--) System.out.print(odd.get(i)+" "); // even 升序 for (Integer i : even) System.out.print(i + " "); } } }
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; /** * Created by fhqplzj on 17-2-2 at 上午9:33. */ public class My1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { ArrayList<Integer> odds = new ArrayList<>(); ArrayList<Integer> evens = new ArrayList<>(); for (int i = 0; i < 10; i++) { int tmp = scanner.nextInt(); if (tmp % 2 == 0) { evens.add(tmp); } else { odds.add(tmp); } } Collections.sort(odds, Collections.reverseOrder()); Collections.sort(evens); StringBuilder stringBuilder = new StringBuilder(); boolean first = true; for (Integer odd : odds) { if (first) { stringBuilder.append(odd); first = false; } else { stringBuilder.append(" ").append(odd); } } for (Integer even : evens) { if (first) { stringBuilder.append(even); first = false; } else { stringBuilder.append(" ").append(even); } } System.out.println(stringBuilder); } } }