最大整数
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;
}
其中,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;
}
相关文章推荐
- URAL 1117
- 51nod 1117 聪明的木匠【贪心+优先队列】
- 1117. Eddington Number(25)
- vijos 1117 数的划分(整数拆分)
- 1117. Eddington Number(25)
- 51nod 1117 聪明的木匠(贪心)
- hdu 1117 Booklet Printing(模拟 分类 水题)
- Hibernate开篇 by Cdy
- Loadrunner11无法启动IE的解决办法
- 软件工程项目组Z.XML会议记录 2013/11/27
- 熬之滴水成石:最想深入了解的内容--windows内核机制(13)
- HTML5实现音频播放
- 学习新知识的方法
- Java 性能优化之 String 篇
- 解决Google Play 此商品无法在您设备所在的国家地区安装的问题
- Fragment的使用与学习及使用过程中遇到的问题
- Centos 编译Apache
- Android App自动更新服务
- hdu oj 2030
- 对View DrawingCache的理解