Codechef Wonder Woman and the Dark man
题目原文
Input
The first line contains 'N' number of numbers formed by the magic cave followed by the sequence of those numbers A1,A2,A3,…An
Output
Print out the largest number formed by the arrangement of boxes.
Constraints
- 1 ≤ N ≤ 106
- 1 ≤ Ai ≤ 106
Example
Input: 6 5 2 1 9 50 56 Output: 95655021
题意分析
给定一组数,将它们进行排列,使得组合得到的数最大。
解法分析
本题实际是对数据进行一次排序,排序规则自定,对于两数a和b,判断将它们连接得到的数ab和ba的大小,大的排在前面,小的在后面。C++代码如下:
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool compareS(string a,string b){
return (a+b)>=(b+a);
}
int main(){
string s;
vector<string> res;
int N;
cin>>N;
for(int i=1;i<=N;i++){
cin>>s;
res.push_back(s);
}
sort(res.begin(),res.end(),compareS);
for(auto r:res)
cout<<r;
return 0;
}