题目1504:把数组排成最小的数

题目描述:

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

 

输入:

输入可能包含多个测试样例。
对于每个测试案例,输入的第一行为一个整数m (1<=m <=100)代表输入的正整数的个数。
输入的第二行包括m个正整数,其中每个正整数不超过10000000。

 

输出:

对应每个测试案例,
输出m个数字能排成的最小数字。

 

样例输入:
3
23 13 6
2
23456 56
样例输出:
13236
2345656


 1 #include <set>  //1504
 2 #include <map>
 3 #include <list>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <deque>
 7 #include <queue>
 8 #include <stack>
 9 #include <cstdio>
10 #include <string>
11 #include <vector>
12 #include <cctype>
13 #include <cstring>
14 #include <sstream>
15 #include <fstream>
16 #include <cstdlib>
17 #include <cassert>
18 #include <iostream>
19 #include <algorithm>
20  
21 using namespace std;
22  
23 inline string getstring ( const int n )
24 {
25     std::stringstream newstr;
26     newstr << n;
27     return newstr.str();
28 }
29  
30 bool cmp(string a, string b)
31 {
32     return a+b < b+a;
33 }
34 int main()
35 {
36     int i; 
37     int n;
38     int arr[105];
39     string str[105];
40      
41     while( cin >> n)
42     {
43         for(i = 0; i < n; i++)
44         {
45             cin >> arr[i];
46             str[i] = getstring(arr[i]);
47         }
48         std::sort(str,str+n,cmp);
49          
50         for(i = 0; i < n; i++)
51             cout<<str[i];
52         cout<<endl;
53     }
54     return 0;
55 }

 

posted @ 2013-12-09 20:12  chchche  阅读(227)  评论(0编辑  收藏  举报