天天看點

杭電ACM 1106 排序 JAVA代碼

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String args[]){
		
		Scanner scan = new Scanner(System.in);
		while(scan.hasNext()){
			String s = scan.nextLine();
			int j = 0;
			//由于split函數不能以首字母分割 把付出串開始的5去掉
			/*
			 * 由于第一遍沒想起來WA了好久 又去百度的别人的思路
			 */
			while(j !=s.length()-1){
				if(s.charAt(j)=='5')
					j++;
				else
					break;
			}
			//生成首字母不為5的字元串
			s = s.substring(j);
			//split 函數 以一個或多個5分割
			String[] str=s.split("5+");
			int[] num = new int[str.length];
			//分割後存進str的是字元串  通過Integer.parseInt逐個變為整數
			for(int i = 0;i<str.length;i++){
				num[i] = Integer.parseInt(str[i]);
				if(num[i] > 100000000)
					System.exit(0);
			}
			//自帶函數
			Arrays.sort(num);
			//輸出
			for(int i = 0 ;i < num.length - 1;i ++){
				System.out.print(num[i]+" ");
			}
			System.out.print(num[num.length-1]);
			System.out.println();
		}

			
	}
}