排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27961 Accepted Submission(s): 7710
Problem Description輸入一行數字,如果我們把這行數字中的‘5’都看成空格,那麼就得到一行用空格分割的若干非負整數(可能有些整數以‘0’開頭,這些頭部的‘0’應該被忽略掉,除非這個整數就是由若干個‘0’組成的,這時這個整數就是0)。
你的任務是:對這些分割得到的整數,依從小到大的順序排序輸出。
Input輸入包含多組測試案例,每組輸入資料只有一行數字(數字之間沒有空格),這行數位長度不大於1000。
輸入資料保證:分割得到的非負整數不會大於100000000;輸入資料不可能全由‘5’組成。
Output對於每個測試案例,輸出分割得到的整數排序的結果,相鄰的兩個整數之間用一個空格分開,每組輸出佔一行。
Sample Input
0051231232050775
Sample Output
0 77 12312320
方法一
import java.util.*;import java.io.*;public class Main {public static void main(String[] args) {Scanner sc=new Scanner(new BufferedInputStream(System.in));while(sc.hasNext()){ArrayList<Integer> ay=new ArrayList<Integer>();String s=sc.next();String str[]=s.split("5");for(String s1:str){if(!s1.equals("")){ay.add(Integer.parseInt(s1));}}Collections.sort(ay);for(int i=0;i<ay.size()-1;i++)System.out.print(ay.get(i)+" ");System.out.println(ay.get(ay.size()-1));}}}
方法二
import java.io.*;import java.math.BigInteger;import java.util.*;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(new BufferedInputStream(System.in));PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out),true);while (sc.hasNext()) {String s = sc.next();sc.nextLine();if (s.charAt(0) == '-')s = s.substring(1);String str[] = s.split("[5]");BigInteger big[] = new BigInteger[str.length];int k = 0;for (int i = 0; i < str.length; i++) {if (!str[i].equals("")) {big[k] = BigInteger.valueOf(Integer.parseInt(str[i]));k++;}}for (int i = 0; i < k; i++) {for (int j = i + 1; j < k; j++) {if (big[i].compareTo(big[j]) == 1) {BigInteger t = big[i];big[i] = big[j];big[j] = t;}}}for (int i = 0; i < k - 1; i++)System.out.print(big[i] + " ");System.out.println(big[k - 1]);}}}