https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1097
设有n个正整数,将它们联接成一排,组成一个最小的多位整数。
例如:
n=2时,2个整数32,321连接成的最小整数为:32132,
n=4时,4个整数55,31,312, 33 联接成的最小整数为:312313355
第2 - N + 1行:每行1个正整数。(1 <= A i
4 55 31 312 33
312313355
一开始想在,后面随便加一个串首值,后来发现了反例
比如1
10
应该是101,不是110,然而我们strcmp的时候,就会有这样的问题。感觉拼接的感觉就是在 看看110还是101大。这就是思路了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
char ans[1110000];
string str[10010];
int cmp(string s1,string s2){
if(s1+s2<s2+s1)
return 1;
return 0;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>str[i];
}
sort(str+1,str+n+1,cmp);
int now=0;
for(int i=1;i<=n;i++){
int len=str[i].length();
for(int j=0;j<len;j++)
ans[now++]=str[i][j];
}
now--;
for(int i=0;i<=now;i++){
printf("%c",ans[i]);
if((i+1)%1000==0)
printf("\n");
}
if((now+1)%n!=0)
printf("\n");
return 0;
}