Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int digitSum(int x) {
- if (x < 10) {
- return x;
- } else {
- return digitSum(x / 10) + x % 10;
- }
- }
- bool cmp(int x, int y) {
- return (digitSum(x) < digitSum(y) || digitSum(x) == digitSum(y) && x < y);
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int n;
- cin >> n;
- int a[n];
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- // [0; n)
- sort(a + 0, a + n, &cmp);
- for (int i = 0; i < n; ++i) {
- cout << a[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement