fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. ifstream fin("sortsum.in");
  7. ofstream fout("sortsum.out");
  8.  
  9. int sc(int x) {
  10. int s = 0;
  11. while(x) {
  12. s += x % 10;
  13. x /= 10;
  14. }
  15. return s;
  16. }
  17.  
  18. bool sum(int a, int b) {
  19. if(sc(a) == sc(b))
  20. return a > b;
  21. return sc(a) < sc(b);
  22. }
  23.  
  24. int v[1000000];
  25.  
  26. int main() {
  27. int i = 1;
  28. while(cin >> v[i++]) {}
  29. i--;
  30. sort(v + 1, v + + i + 1, sum);
  31. for(int ii = 2; ii <= i; ii++) {
  32. cout << v[ii] << ' ';
  33. }
  34. }
  35.  
Success #stdin #stdout 0s 5320KB
stdin
102 60 51 600 21 3
stdout
102 21 3 600 60 51