解题思路:暴力
注意事项:无
参考代码:
#pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define endl '\n' #define int ll struct node { int a; int b; } s[1000005];//定义结构体存放(数字 a)和(数位和 b) int sum(int a) {//计算数位和 int m = 0; while(a) { m += a % 10; a /= 10; } return m; } bool cmp(node& x, node& y) { if (x.b < y.b) {//数位和小的在前面 return true; } else if (x.b == y.b) { //数位和相等,数字小的在前面 if(x.a < y.a) { return true; } } return false;//否则反之 } void solve() { int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { //输入数字的同时计算数位和 s[i].a = i; s[i].b = sum(i); } sort(s + 1, s + n + 1, cmp);//调用sort采用题目要求的cmp来比较 // for(int i = 1; i <= n; i++) {//输出测试 // cout << s[i].a << " " << s[i].b << endl; // } cout << s[m].a << endl;//输出第m个 } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int t = 1; //cin >> t; while (t--) solve(); return 0; }
0.0分
2 人评分