首先要知道什么是数位之和
其实就是 数的每一位上数字的和
那就很ez了 只需要把每一位的数字分离开来 记录一下就好
排序的时候 如果数位之和相同 记得返回原数较小的那一个
时间复杂度
最多也就 O(6*n) 轻松过
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
struct node{
int bef;
int seat;
};
node num[N];
bool cmp(node a,node b){
if(a.seat==b.seat) return a.bef<b.bef;
return a.seat<b.seat;
}
int main(){
int n,m;
cin>>n;
for(int i=1;i<=n;i++){
int x,ans=0;
x=i;
while(x){
ans+=x%10;
x/=10;
}
num[i].seat=ans,num[i].bef=i;
}
sort(num+1,num+n+1,cmp);
cin>>m;
cout<<num[m].bef<<endl;
return 0;
}