蓝桥杯 AcWing 4653. 数位排序
作者:
闪回
,
2024-03-12 15:02:14
,
所有人可见
,
阅读 19
用pair存数位之和和原数,重写sort的cmp比较函数即可
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e6+10;
int n,m;
PII a[N];
#define x first
#define y second
int get_num(int x)
{
int res = 0;
while(x)
{
res += x % 10;
x /= 10;
}
return res;
}
bool cmp(PII a,PII b)
{
if(a.x != b.x)return a.x < b.x;
return a.y < b.y;
}
int main()
{
cin>>n>>m;
for (int i = 1; i <= n; i ++ )
{
int sum = get_num(i);
a[i] = {sum,i};
}
sort(a+1,a+1+n,cmp);
cout<<a[m].y<<endl;
return 0;
}