数位排序(qsort使用方法)
#include <stdio.h>
#include <stdlib.h>
#define N (int)(1e6 + 10)
struct number{
int ds,value;
}num[N];
int change(int x)
{
int res = 0;
while(x)
{
res += x % 10;
x /= 10;
}
return res;
}
int cmp(const void* a, const void *b)
{
struct number* aa = (struct number*)a;
struct number* bb = (struct number*)b;
if(aa->ds > bb->ds) return 1;
else if(aa->ds == bb->ds){
if(aa->value > bb->value) return 1;
else return -1;
}
return -1;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++){
num[i].ds = change(i);
num[i].value = i;
}
qsort(num + 1, n, sizeof(num[0]),cmp);
printf("%d\n",num[m].value);
return 0;
}