08-3. 組個最小數 (20)

來源:互聯網
上載者:User

標籤:style   blog   color   使用   strong   io   for   div   

給定數字0-9各若干個。你可以以任意順序排列這些數字,但必須全部使用。目標是使得最後得到的數儘可能小(注意0不能做首位)。例如:給定兩個0,兩個1,三個5,一個8,我們得到的最小的數就是10015558。

現給定數字,請編寫程式輸出能夠組成的最小的數。

輸入格式:

每個輸入包含1個測試案例。每個測試案例在一行中給出10個非負整數,順序表示我們擁有數字0、數字1、……數字9的個數。整數間用一個空格分隔。10個數位總個數不超過50,且至少擁有1個非0的數字。

輸出格式:

在一行中輸出能夠組成的最小的數。

輸入範例:

2 2 0 0 0 3 0 0 1 0

輸出範例:

10015558



 1 #include <stdio.h> 2  3 int main() 4 { 5     int a[10], i, flag = 1; 6     for(i = 0; i < 10; i++) { 7         scanf("%d", &a[i]);                          //將數字i 出現的次數存放在a 數組裡  8         if(flag == 1 && i != 0 && a[i] != 0) {       //找到除數字 0 外且會出現的數字中的最小的   9             printf("%d", i);                         //並輸出它 10             a[i] -= 1;                               //然後讓它的次數減一 11             flag = 0;12         }13     }14     int j;15     for(i = 0; i < 10; i++) {                        //遍曆a[i] 16         for(j = 0; j < a[i]; j++) {                  //迴圈a[i]次 17             printf("%d", i);                         //輸出a[i]個 i 18         }19     }20     21     return 0;22 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.