JosepRivaille

P21895: Nombres estrobogramĂ tics

Apr 9th, 2015
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int count_digits(int n) {
  7.     int dig = 0;
  8.     while (n != 0) {
  9.         n = n/10;
  10.         ++dig;
  11.     }
  12.     return dig;
  13. }
  14.  
  15. bool es_estrobogramatic(int n) {
  16.     if (n < 10) {
  17.         if (n == 1 or n == 0 or n == 8) return true;
  18.         return false;
  19.     }
  20.     else {
  21.         int digits = count_digits(n);
  22.         vector<int> estrobo(digits);
  23.         for (int i = digits-1; i >= 0; --i) {
  24.             if (n%10 == 2 or n%10 == 3 or n%10 == 4 or n%10 == 7) return false;
  25.             estrobo[i] = n%10;
  26.             n = n/10;
  27.         }
  28.         int i = 0;
  29.         int j = digits-1;
  30.         while (i <= j) {
  31.             if (estrobo[i] == estrobo[j] && (estrobo[i] == 0 or estrobo[i] == 1 or estrobo[i] == 8));
  32.             else if ((estrobo[i] == 6 && estrobo [j] == 9) or (estrobo[i] == 9 && estrobo[j] == 6));
  33.             else return false;
  34.             ++i;
  35.             --j;
  36.         }
  37.         return true;
  38.     }
  39. }
  40.  
  41.  
  42. int main() {
  43.     int n;
  44.     int count_senar = 0;
  45.     while (cin >> n) {
  46.         if (es_estrobogramatic(n)) cout << n << " si es estrobogramatic" << endl;
  47.         else if (not es_estrobogramatic(n)) cout << n << " no es estrobogramatic" << endl;
  48.         if (es_estrobogramatic(n) && n%2 != 0) ++count_senar;
  49.     }
  50.     cout << endl << "estrobogramatics senars: " << count_senar << endl;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment