直接对string数组进行排序。需要注意的是,当string长度相等时,进行字典序排序,否则比较两字串进行组合的序列来排序;
其中,str.c_str是将string类型转换成char,atoi是将char转换成int型;
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 3005;
vector< string > str;
string temp;
bool cmp( string a, string b ){
if( a.length() == b.length() ){
return a < b;
}
string str1 = a + b;
string str2 = b + a;
return atoi( str1.c_str() ) < atoi( str2.c_str() );
}
int main(){
int Case, n;
scanf( "%d", &Case );
while( Case-- ){
str.clear();
scanf( "%d", &n );
for( int i = 0; i < n; ++i ){
cin >> temp;
str.push_back( temp );
}
sort( str.begin(), str.end(), cmp );
for( int i = n - 1; i >= 0; --i ){
cout << str[ i ];
}
puts( "" );
}
return 0;
}