Sort sum of digit
unknown
c_cpp
2 years ago
580 B
27
Indexable
#include <bits/stdc++.h>
#include <cstring>
#define f first
#define s second
#define ll long long
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()
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n, &cmp);
for (int i = 0; i < n; ++i) {
cout << a[i] << " ";
}
return 0;
}
Editor is loading...
Leave a Comment