Advertisement
halexandru11

prima problema

Nov 29th, 2020
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned int suma(unsigned int x)
  6. {
  7.     unsigned int s = 0;
  8.     while(x > 0)
  9.     {
  10.         s = s + x%10;
  11.         x = x/10;
  12.     }
  13.     return s;
  14. }
  15.  
  16. int main()
  17. {
  18.     unsigned int n, a[100];
  19.     cin >> n;
  20.     for(int i = 0; i < n; ++i)
  21.     {
  22.         cin >> a[i];
  23.     }
  24.  
  25.     for(int i = 0; i < n-1; ++i)
  26.     {
  27.         for(int j = i+1; j < n; ++j)
  28.         {
  29.             if(suma(a[i]) > suma(a[j]))
  30.             {
  31.                 int aux = a[i];
  32.                 a[i] = a[j];
  33.                 a[j] = aux;
  34.             }
  35.         }
  36.     }
  37.  
  38.     for(int i = 0; i < n; ++i)
  39.     {
  40.         cout << a[i] << " ";
  41.     }
  42.     return 0;
  43. }
  44.  
Advertisement
RAW Paste Data Copied
Advertisement