Advertisement
Josif_tepe

Untitled

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