【字符串】P1012 拼数

题解:

https://capw-need-rescue.blog.luogu.org/p1012-pin-shuo

题解代码:

#include<iostream>
#include<string>
#include<algorithm>//提供sort 
using namespace std;
string s[25];//不多说 
int n;//限制数字个数 

bool cmp(string a,string b) {
    return a+b>b+a;//自定义排序函数,这一步非常巧妙,假设a=321,b=32;a+b=32132,b+a=32321这样下面sort排下来就是32>321避免出现32132>32321的情况 
}
/*如果这样写:
bool cmp(string a,string b){
    return a>b;
    会发生321>32的情况,具体原因是字符串自己的关系运算是这样设定的 
}*/
int main() {
    cin>>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];//完美收场(yo)! 
    return 0;
}
原文地址:https://www.cnblogs.com/infocodez/p/13951685.html