题目链接
思路:
- 用
string容器
存储字符串,开一个string str[maxn]
的空间把输入的字串储存。 - 写一个
cmp
函数,用sort函数
对这些string
排序。 - 排好后连接成一个目标字符串
string ans
,并去掉ans
的前导0。 - 输出:去掉前导0都的
ans
若为空,则输出0, 不为空则输出ans
。
AC代码
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
const int maxn = 10010;
string str[maxn];
int n;
bool cmp(string s1, string s2){
string temp1 = s1 + s2;
string temp2 = s2 + s1;
return temp1 < temp2;
}
int main()
{
scanf("%d ",&n);
for(int i=0; i<n; i++){
cin>>str[i];
}
sort(str, str+n, cmp);
string ans;
for(int i=0; i<n; i++){
ans += str[i];
}
while(ans.size()!='0' && ans[0] == '0' ){
ans.erase(ans.begin());
}
if(ans.size() == 0){
printf("0");
}
else{
cout<<ans;
}
return 0;
}