题解 | #拼数#

拼数

https://ac.nowcoder.com/acm/problem/16783

贪心题
对于序列中的两个数,交换顺序不改变其他数的值,所以要选择两种顺序较大的那一个。
直接a+b>b+a就行了
写题解之前一时间没想出来这种解法,想的是比较最高位的数来排序,这其实也可以,但当位数不同时要考虑的情况其实也是上面的情况。

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(string a,string b)
{
    a+b>b+a;
}
string a[100];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)cout<<a[i];
    cout<<endl;
}
全部评论

相关推荐

05-12 17:28
已编辑
门头沟学院 硬件开发
ldf李鑫:不说公司名祝你以后天天遇到这样的公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务