Advertisement
JosepRivaille

X50141: Funció per engreixar nombres

Mar 17th, 2015
1,537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int engreixa(int x) {
  6.     int count = 0;
  7.     for (int i = x; i != 0; i = i/10) ++count;
  8.     int ix; //inversa x
  9.     ix = x%10;
  10.     x = x/10;
  11.     for (int i = x; i != 0; i = i/10) ix = ix*10 + i%10;
  12.     x = ix%10;
  13.     ix = ix/10;
  14.     int aux = x;
  15.     --count;
  16.     while (ix != 0 or count != 0) {
  17.         if (ix%10 > aux) {
  18.             x = x*10 + ix%10;
  19.             aux = ix%10;
  20.         }
  21.         else x = x*10 + aux;
  22.         ix = ix/10;
  23.         --count;
  24.     }
  25.     return x;
  26. }
  27.    
  28. //Pre: Llegeix un nombre
  29. //Post: Engreixa el nombre
  30. //Engreixar és canviar la següent xifra per l'actual
  31. //si la següent és més petita
  32. int main() {
  33.     int x;
  34.     cin >> x;
  35.     cout << engreixa(x) << endl;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement