题意:给定n个正整数,把它们连接成一个最大的整数.比如,123,124,556,90有24种连接方法,最大的结果为9 056 124 123。
贪心。一开始就想用string水过。注意不能直接用string的一般默认比较方式(字典序),如90和9,应该比较x+y与y+x
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=50+5;
int n;
string s[maxn];
int aa;char cc;
int read() {
aa=0;cc=getchar();
while(cc<'0'||cc>'9') cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
return aa;
}
bool cmp(const string x,const string y) {
return x+y>y+x;
}
int main() {
n=read();
while(n){
for(int i=1;i<=n;++i) cin>>s[i];
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;++i) cout<<s[i];
printf("\n");
n=read();
}
return 0;
}