本文主要是介绍蓝桥杯备战9.拼数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
P1012 [NOIP1998 提高组] 拼数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
这道题作对了,但是题解看到更好的
我写的史
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e7+10,M = 1e3+10;
int a[N],h[N];
struct birthday
{string name;int y,m,d,id;
};bool cmp(int a,int b)
{string s1 = to_string(a);string s2 = to_string(b);int m = min(s1.size(),s2.size());for(int i=0;i<m;i++){if(s1[i]>s2[i]){return 1;}else if(s1[i]<s2[i])return 0;}if(s1.size()>s2.size()){if(s1[m]>s2[m-1])return 1;else return 0;}else{//s2>s1if(s1[m-1]>s2[m])return 1;else return 0;}
}
signed main()
{std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);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];}return 0;
}
cmp函数优化之后
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e7+10,M = 1e3+10;
int a[N],h[N];
struct birthday
{string name;int y,m,d,id;
};bool cmp(int a,int b)
{string s1 = to_string(a);string s2 = to_string(b);return (s1+s2>s2+s1);
}
signed main()
{std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);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];}return 0;
}
这篇关于蓝桥杯备战9.拼数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!