Untitled

mail@pastecode.io avatar
unknown
c_cpp
24 days ago
1.1 kB
1
Indexable
Never
#include <iostream>
#include <vector>
using namespace std;
 
int main() {
    int n;
    cin >> n;
    int k;
    cin >> k;
    vector<int> v;
    for(int i = 0; i < 100; i++) {
        int tmp = n;
        int sum = 0;
        v.push_back(n);
        while(tmp > 0) {
            int cifra = tmp % 10;
            tmp /= 10;
            sum += cifra * cifra;
        }
        n = sum;
        if(i + 1 == k) {
            cout << sum << endl;
            return 0;
        }
    }
    vector<int> ciklus;
    int ok = 0;
    for(int i = 0; i < v.size(); i++) {
        
        for(int j = i + 1; j < v.size(); j++) {
            if(v[i] == v[j]) {
                ok = 1;
                for(int k = i; k < j; k++) {
                    ciklus.push_back(v[k]);
                }
                break;
            }
        }
        if(ok == 1) {
            k -= i;
            break;
        }
    }
    int sz = ciklus.size();
    int ost = k % sz;
    if(ost == 0) {
        cout << ciklus[sz - 1] << endl;
    }
    else {
        cout << ciklus[ost] << endl;
    }
    return 0;
}
Leave a Comment