Advertisement
JosepRivaille

P69781: Pseudo-seqüències de Collatz (2)

Feb 19th, 2016
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5.  
  6. int main() {
  7.   int x, y, n;
  8.   while (cin >> x >> y >> n) {
  9.     map<int,int> M;
  10.     map<int,int>::iterator it;
  11.     int i = 0;
  12.     bool found = false;
  13.     M.insert(make_pair(n, i));
  14.     while (n <= 100000000 && !found) {
  15.       ++i;
  16.       if (n%2 == 0) n = n/2 + x;
  17.       else n = 3*n + y;
  18.       it = M.find(n);
  19.       if (it == M.end()) M.insert(make_pair(n, i));
  20.       else found = true;
  21.     }
  22.     if (found) cout << (M.size() - it->second) << endl;
  23.     else cout << n << endl;
  24.   }
  25. }
  26.  
  27. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement