Advertisement
Josif_tepe

Untitled

Mar 2nd, 2024
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int main() {
  5.     int p, n;
  6.     cin >> p >> n;
  7.    
  8.     int niza[200], j = 0;
  9.     for(int i = 0; i < 100; i++) {
  10.         int tmp = p;
  11.         int sum = 0;
  12.         while(tmp > 0) {
  13.             int cifra = tmp % 10;
  14.             cifra *= cifra;
  15.             sum += cifra;
  16.            
  17.             tmp /= 10;
  18.         }
  19.         if(i == n) {
  20.             cout << p << endl;
  21.             return 0;
  22.         }
  23.         niza[j] = p;
  24.         j++;
  25.         p = sum;
  26.     }
  27.     int povtoruvanja[200], o = 0;
  28.     for(int i = 0; i < j; i++) {
  29.         int se_povtoruva = -1;
  30.         for(int k = i + 1; k < j; k++) {
  31.             if(niza[i] == niza[k]) {
  32.                 se_povtoruva = k - 1;
  33.                 break;
  34.             }
  35.         }
  36.         if(se_povtoruva != -1) {
  37.             n -= i;
  38.             for(int k = i; k <= se_povtoruva; k++) {
  39.                 povtoruvanja[o] = niza[k];
  40.                 o++;
  41.             }
  42.             break;
  43.         }
  44.     }
  45.    
  46.     if(n % o == 0) {
  47.         cout << povtoruvanja[0] << endl;
  48.     }
  49.     else {
  50.         cout << povtoruvanja[n % o] << endl;
  51.     }
  52.     return 0;
  53. }
  54.  
  55. //n = 27 - 4 = 23
  56. // 89 145 42 20 4 16 37 58
  57. // 23 % 8 = 7
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement