Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main() {
- int p, n;
- cin >> p >> n;
- int niza[200], j = 0;
- for(int i = 0; i < 100; i++) {
- int tmp = p;
- int sum = 0;
- while(tmp > 0) {
- int cifra = tmp % 10;
- cifra *= cifra;
- sum += cifra;
- tmp /= 10;
- }
- if(i == n) {
- cout << p << endl;
- return 0;
- }
- niza[j] = p;
- j++;
- p = sum;
- }
- int povtoruvanja[200], o = 0;
- for(int i = 0; i < j; i++) {
- int se_povtoruva = -1;
- for(int k = i + 1; k < j; k++) {
- if(niza[i] == niza[k]) {
- se_povtoruva = k - 1;
- break;
- }
- }
- if(se_povtoruva != -1) {
- n -= i;
- for(int k = i; k <= se_povtoruva; k++) {
- povtoruvanja[o] = niza[k];
- o++;
- }
- break;
- }
- }
- if(n % o == 0) {
- cout << povtoruvanja[0] << endl;
- }
- else {
- cout << povtoruvanja[n % o] << endl;
- }
- return 0;
- }
- //n = 27 - 4 = 23
- // 89 145 42 20 4 16 37 58
- // 23 % 8 = 7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement