Advertisement
JosepRivaille

P32149: Nombres ascendents

Apr 6th, 2015
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. //Un nombre es ascendent si ala dreta d'una xifra hi ha la mateixa xifra +1
  6. //Si la xifra es 9 ha d'haver-hi un 0
  7. bool es_ascendent(int n) {
  8.     int comp = n%10;
  9.     n = n/10;
  10.     while (n != 0) {
  11.         if ((n%10 + 1)%10 != comp) return false;
  12.         else {
  13.             comp = n%10;
  14.             n = n/10;
  15.         }
  16.     }
  17.     return true;
  18. }
  19.  
  20. //Pre: Llegeix un natural
  21. //Post: Escriu si es ascendent
  22. int main() {
  23.     bool s2 = false;
  24.     int x, fi = 0;
  25.     while (not s2 && cin >> x) {
  26.         if (fi == 0) {
  27.             if (es_ascendent(x)) ++fi;
  28.         }
  29.         else if (fi == 1) {
  30.             if (es_ascendent(x)) ++fi;
  31.             else --fi;
  32.         }
  33.         if (fi == 2) s2 = true;
  34.     }
  35.     if (fi == 2) cout << "SI" << endl;
  36.     else cout << "NO" << endl;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement