【PAT】1023 組個最小數 (20 分)
阿新 • • 發佈:2019-01-06
1023 組個最小數 (20 分)
給定數字 0-9 各若干個。你可以以任意順序排列這些數字,但必須全部使用。目標是使得最後得到的數儘可能小(注意 0 不能做首位)。例如:給定兩個 0,兩個 1,三個 5,一個 8,我們得到的最小的數就是 10015558。
現給定數字,請編寫程式輸出能夠組成的最小的數。
輸入格式:
輸入在一行中給出 10 個非負整數,順序表示我們擁有數字 0、數字 1、……數字 9 的個數。整數間用一個空格分隔。10 個數字的總個數不超過 50,且至少擁有 1 個非 0 的數字。
輸出格式:
在一行中輸出能夠組成的最小的數。
輸入樣例:
2 2 0 0 0 3 0 0 1 0
輸出樣例:
10015558
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] in = br.readLine().split(" "); boolean b = true; for(int i = 1; i < in.length;i++){ if(Integer.parseInt(in[i]) > 0 && b == true){ b = false; System.out.print(i); if(Integer.parseInt(in[0]) > 0){ for(int j = 0; j < Integer.parseInt(in[0]);j++) System.out.print("0"); } for(int k = 1;k<Integer.parseInt(in[i]);k++) System.out.print(i); }else if(Integer.parseInt(in[i]) > 0){ for(int k = 0;k<Integer.parseInt(in[i]);k++) System.out.print(i); } } } }