Advertisement
monito2207

d1_2

Oct 31st, 2021 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. /**
  2. *
  3. * Solution to homework assignment 1
  4. * Introduction to programming course
  5. * Faculty of Mathematics and Informatics of Sofia University
  6. * Winter semester 2021/2022
  7. *
  8. * @author Monika Dobrinova
  9. * @idnumber 8MI0600008
  10. * @task 2
  11. * @compiler VC
  12. *
  13. */
  14.  
  15. #include <iostream>
  16. using namespace std;
  17. int main()
  18.  
  19. {
  20.     int n;
  21.     cin >> n;
  22.  
  23.     if (n >= 1 && n <=9)
  24.     {
  25.         cout << n << endl;
  26.         return 0;
  27.     }
  28.  
  29.     int temp = n;
  30.     int count = 0;
  31.  
  32.     while (temp)
  33.     {
  34.         temp /= 10;
  35.         count++;
  36.     }
  37.  
  38.     int pow = 1;
  39.     int left = 0;
  40.     int right = 0;
  41.  
  42.     for (int i = 1; i < count; i++)
  43.     {
  44.         pow *= 10;
  45.     }
  46.  
  47.     while (n)
  48.     {
  49.         right = n % 10;
  50.         left = n / pow;
  51.  
  52.  
  53.         if (left > right)
  54.         {
  55.             cout << left << " ";
  56.         }
  57.  
  58.         else if (right > left)
  59.         {
  60.             cout << right << " ";
  61.         }
  62.  
  63.         if (pow == 1)
  64.         {
  65.             cout << n << " ";
  66.             break;
  67.         }
  68.  
  69.         n %= pow;
  70.         n /= 10;
  71.         pow /= 100;
  72.     }
  73.         cout << endl;
  74.  
  75.         return 0;
  76.     }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement