解题思路:
创建结构体用number存储数值,len存储各各数位相加的结果。根据题意定义小于运算,然后可以直接使用c++内置的sort函数进行排序。
注意事项:
参考代码:
#include<bits/stdc++.h> using namespace std; struct node{ int number; int len; }; bool operator < (node& A, node& B) { if(A.len<B.len)return true; else if (A.len == B.len && A.number < B.number) return true; else return false; } struct node line[1000005]; int main() { int i,j,m,n; cin >> n >> m; for(i=1;i<=n;i++) { line[i].number=i; line[i].len=0; } for(i=1;i<=n;i++) { int ink = line[i].number; while(ink>0) { line[i].len = line[i].len + (ink % 10); ink = ink / 10; } } sort(line+1,line+n+1); cout << line[m].number << endl; return 0; }
0.0分
2 人评分
输出九九乘法表 (C语言代码)浏览:555 |
简单的a+b (C++语言代码)浏览:853 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:702 |
WU-整数平均值 (C++代码)浏览:1237 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:685 |
C语言训练-亲密数 (C语言代码)浏览:682 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:484 |
矩阵转置 (C语言代码)浏览:782 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:455 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:535 |