Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int kvadratiNaCifri(int broj) {
- int zbir = 0;
- while (broj > 0) {
- int cifra = broj % 10;
- zbir += cifra * cifra;
- broj /= 10;
- }
- return zbir;
- }
- int main() {
- int x,n;
- cin>>x>>n;
- vector<int> v;
- int first_number = x;
- for(int i=1;i<=100;i++){
- v.push_back(first_number);
- first_number=kvadratiNaCifri(first_number);
- if(i == n) {
- cout << first_number << endl;
- return 0;
- }
- }
- vector<int> repetion;
- for(int i = 0; i < v.size(); i++) {
- int se_povtoruva = -1;
- for(int j = i + 1; j < v.size(); j++) {
- if(v[i] == v[j]) {
- se_povtoruva = j;
- break;
- }
- }
- if(se_povtoruva != -1) {
- n -= i;
- for(int j = i; j < se_povtoruva; j++) {
- repetion.push_back(v[j]);
- }
- break;
- }
- }
- if(n % repetion.size() == 0) {
- cout << repetion[repetion.size() - 1] << endl;
- }
- else {
- cout << repetion[n % repetion.size()] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement