JosepRivaille

X87201: Nombres rodons (2)

Apr 9th, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. long long canvi_base(int n, int b) {
  6.     long long nb = n%b;
  7.     int mult = 1;
  8.     n = n/b;
  9.     while (n != 0) {
  10.         mult = 10 * mult;
  11.         nb = nb + mult * (n%b);
  12.         n = n/b;
  13.     }
  14.     return nb;
  15. }
  16.  
  17. bool rodo(int n, int b) {
  18.     int n1 = canvi_base(n, b);
  19.     int count = 0;
  20.     int suma = 0;
  21.     while (n1 != 0) {
  22.         suma = suma + n1%10;
  23.         n1 = n1/10;
  24.         ++count;
  25.     }
  26.     if (suma == count) return true;
  27.     return false;
  28. }
  29.  
  30. int main() {
  31.     int n, b;
  32.     int count = 0;
  33.     bool fi = false;
  34.     while (not fi && cin >> n >> b) {
  35.         if (rodo(n, b)) {
  36.             if (count == 0) ++count;
  37.             else if (count == 1) ++count;
  38.             else fi = true;
  39.         }
  40.     }
  41.     if (count == 2) cout << "SI" << endl;
  42.     else cout << "NO" << endl;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment