Sort - B

public
yeskendir.sultanov Mar 31, 2024 Never 43
Clone
C++ Bsort.cpp 37 lines (29 loc) | 652 Bytes
1
#include <bits/stdc++.h>
2
3
using namespace std;
4
5
int digitSum(int x) {
6
if (x < 10) {
7
return x;
8
} else {
9
return digitSum(x / 10) + x % 10;
10
}
11
}
12
13
bool cmp(int x, int y) {
14
return (digitSum(x) < digitSum(y) || digitSum(x) == digitSum(y) && x < y);
15
}
16
17
int main() {
18
std::ios_base::sync_with_stdio(false);
19
cin.tie(0);
20
cout.tie(0);
21
22
int n;
23
cin >> n;
24
int a[n];
25
for (int i = 0; i < n; ++i) {
26
cin >> a[i];
27
}
28
29
// [0; n)
30
sort(a + 0, a + n, &cmp);
31
32
for (int i = 0; i < n; ++i) {
33
cout << a[i] << " ";
34
}
35
36
return 0;
37
}
38
39