参考博客:https://blog.csdn.net/sinat_29278271/article/details/48047877
思路:要求组合的数字最小,即片段组合的字典序最小,对于两个相邻的片段 a和b,如果a+b<b+a,则将其交换,则最终必定会有一个组合序列,使其不能再交换。若我们将这个交换过程看做是排序的过程,则可以使用sort函数来实现。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string numlist[10001];
bool cmp(string a, string b)
{
return a+b < b+a;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> numlist[i];
sort(numlist, numlist+n, cmp);
string ans;
for(int i = 0; i < n; i++)
ans += numlist[i];
int i = 0;
for(; i < ans.size(); i++)
if(ans[i] != '0')
break;
if(i == ans.size())
cout << 0 << endl;
else
cout << ans.substr(i) << endl;
return 0;
}