Advertisement
Josif_tepe

Untitled

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