Advertisement
apl-mhd

3n+1 Time limit exit

Jun 30th, 2021
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4.  
  5. using  namespace  std;
  6. int main() {
  7.  
  8.  
  9.     int memo[1000000] = {};
  10.  
  11.  
  12.     while (true) {
  13.         int cycle = 0;
  14.  
  15.         int m,n;
  16.         cin>>m>>n;
  17.  
  18.         for (int i = m; i <= n; ++i) {
  19.  
  20.             int n = i;
  21.             // cout << n << " ";
  22.  
  23.             int count = 1;
  24.  
  25.  
  26.             if (memo[n] == 0) {
  27.  
  28.                 while (n != 1) {
  29.  
  30.                     //if (memo[n] != 0) {
  31.  
  32.                         if (n % 2 == 1)
  33.                             n = 3 * n + 1;
  34.                         else
  35.                             n = n / 2;
  36.                         //  cout << n << " ";
  37.                         count++;
  38.                    // } else {
  39.                         //count += memo[n];
  40.                       //  break;
  41.                     //}
  42.                 }
  43.             }
  44.             memo[i] = count;
  45.  
  46.             // cout << ": " << count<<endl;
  47.             if (cycle < count)
  48.                 cycle = count;
  49.  
  50.         }
  51.         cout << m << " " << n << " " << cycle << endl;
  52.  
  53.         for (int i = 1; i <=1000 ; ++i) {
  54.             cout<<i<<" : "<<memo[i]<<", ";
  55.         }
  56.     }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement