您的位置:首页 > 其它

最大整数

2013-11-30 00:12 148 查看
直接对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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  1117