最大整数


现在有 n 个正整数,将他们连成一排,组成一个最大的整数。

例如,现在有三个整数 13, 312, 343,连接成最大整数为 34331213。

输入

第一行一个整数 n。(1 ≤ n ≤ 100000)

第二行 n 个不超过 int 类型范围的正整数。

输出

输出一个数表示组成的最大整数。

样例

1
2
3
121 12 96
1
9612121

参考代码

思路:按照某种规则进行排序(前面的字符串 连接上 后面的字符串的字典序,大于后面的字符串连接上前面的字符串的字典序)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int n;
string str[100005];

bool cmp(const string &str1, const string &str2) {
//前面连接上后面的字典序 大于 后面连接上前面的字典序
return str1 + str2 > str2 + str1;
}

int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> str[i];
sort(str, str + n, cmp);
for (int i = 0; i < n; ++i) cout << str[i];
cout << endl;
return 0;
}