JosepRivaille

X33736: Nombres narcisistes

Apr 9th, 2015
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int exponent(int n) {
  6.     int exp = 1;
  7.     int suma = 0;
  8.     int n1, sumap;
  9.     int aux = n;
  10.     int pot = exp;
  11.     while (suma < aux) {
  12.         exp = pot;
  13.         n1 = n%10;
  14.         sumap = n1;
  15.         n = n/10;
  16.         --exp;
  17.         for (int i = exp; i != 0; --i) {
  18.             sumap = sumap * n1;
  19.         }
  20.         suma = suma + sumap;
  21.         if (n == 0) {
  22.             ++pot;
  23.             n = aux;
  24.             suma = 0;
  25.         }
  26.     }
  27.     --pot;
  28.     return pot;
  29. }
  30.  
  31. bool es_narcicista(int n) {
  32.     if (n >= 10) {
  33.         int k = exponent(n);
  34.         int n1, n2;
  35.         int aux = n;
  36.         int suma = 0;
  37.         while (n != 0) {
  38.             int i = k;
  39.             n1 = n%10;
  40.             n2 = n1;
  41.             n = n/10;
  42.             --i;
  43.             while (i != 0) {
  44.                 n1 = n1 * n2;
  45.                 --i;
  46.             }
  47.             suma = suma + n1;
  48.         }
  49.         if (suma == aux) return true;
  50.         return false;
  51.     }
  52.     else return true;
  53. }
  54.  
  55.  
  56. int main() {
  57.     int n;
  58.     bool narc = true;
  59.     while (cin >> n && narc) {
  60.         if (not es_narcicista(n)) narc = false;
  61.     }
  62.     if (narc) cout << "SI" << endl;
  63.     else cout << "NO" << endl;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment