题解:#1121.拼数 审核通过

sunshine302 2025-11-15 10:45:55 2025-11-16 8:55:38 5

字符串排序题

思路: 可以把这n个正整数看成字符串,并排序,前面的数字越大,拼出的数越大。

但该如何排序呢? 举个例子: 输入:2 33 34 看将谁拼在前时,拼出的数更大 3433<3334 所以最大数是3433

也就是说:排序比较的是s1+s2(3334)和s2+s1(3433)的大小

代码: #include <bits/stdc++.h>

using namespace std;

int n;

const int N=25;

string a[N];

bool cmp(string a,string b){

return a+b>b+a;

}

int main(){

cin>>n;

for(int i=1;i<=n;i++){

	cin>>a[i];

}

sort(a+1,a+n+1,cmp);//快排函数

for(int i=1;i<=n;i++){

	cout<<a[i];

}

return 0;

}

{{ vote && vote.total.up }}