DMG

Srećan broj

DMG
Jul 3rd, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int  suma (int a)
  6. {
  7.      int k=0;
  8.      
  9.      while (a>0)
  10.      {
  11.            k = k + (a%10)*(a%10);
  12.            a = a / 10;
  13.      }
  14.      
  15.      return k;
  16. }
  17.  
  18. bool srecan (int n)
  19. {
  20.       bool p = false;
  21.       int t, s, i;
  22.      
  23.       t = suma(n);
  24.       if (t==1)
  25.       p = true;
  26.      
  27.       s = t;
  28.       i = n;
  29.      
  30.       do
  31.       {
  32.             s = suma(s);
  33.             if (s==1)
  34.             p = true;
  35.             i = i - 1;
  36.       }
  37.       while ((s!=t)&&(s!=1)&&(i>0));
  38.      
  39.       return p;
  40. }
  41.  
  42. main()
  43. {
  44.       int n, x = 0, i;
  45.      
  46.       cin >> n;
  47.      
  48.       i = n - 1;
  49.      
  50.       while ((i>0)&&(x==0))
  51.       {
  52.             if (srecan (i) == true)
  53.             x = i;
  54.             i = i - 1;
  55.       }
  56.  
  57.       cout << x << endl;
  58.      
  59.       system ("PAUSE");
  60. }
Advertisement
Add Comment
Please, Sign In to add comment