JosepRivaille

P79123: Nombres diabòlics

Apr 9th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. long long base_4(long long n) {
  6.     long long n4 = n%4;
  7.     n = n/4;
  8.     long long mult = 1;
  9.     while (n != 0) {
  10.         mult = mult*10;
  11.         n4 = n4 + mult*(n%4);
  12.         n = n/4;
  13.     }
  14.     return n4;
  15. }
  16.  
  17. int suma_digits(long long n) {
  18.     long long s = 0;
  19.     while (n != 0) {
  20.         s = s + n%10;
  21.         n = n/10;
  22.     }
  23.     s = 2*s;
  24.     return s;
  25. }
  26.  
  27. bool es_diabolic(int n) {
  28.     if (n%suma_digits(base_4(n)) == 0) return true;
  29.     else return false;
  30. }
  31.  
  32. int main() {
  33.     int n, count = 0;
  34.     while (cin >> n && n != -1) {
  35.         if (es_diabolic(n)) ++count;
  36.     }
  37.     int digits = 0;
  38.     for (int i = count; i != 0; i = i/10) {
  39.         ++digits;
  40.     }
  41.     if (digits == 6) cout << count << endl;
  42.     else if (digits == 5) cout << '0' << count << endl;
  43.     else if (digits == 4) cout << "00" << count << endl;
  44.     else if (digits == 3) cout << "000" << count << endl;
  45.     else if (digits == 2) cout << "0000" << count << endl;
  46.     else if (digits == 1) cout << "00000" << count << endl;
  47.     else cout << "000000" << endl;
  48.  }
Advertisement
Add Comment
Please, Sign In to add comment