C ++ (3n + 1) conjecture that kills people unpaid

The first time to brush PAT, note: Just like ordinary compilers, you must import header files and using namespace std; otherwise you will get an error (ghost knows what smile I experienced.jpg)

1 #include <iostream>
 2  
3  using  namespace std;
 4  
5  int main ()
 6  {
 7      int time = 0 ;
 8      int n;
 9      cin >> n;
 10      while (n! = 1 ) {
 11          if (n% 2 == 0 )
 12          { // Even half cut 
13              n / = 2 ;
 14          } else 
15          {// Odd 3 * n + 1 cut in half 
16              n = ( 3 * n + 1 ) / 2 ;
 17          }
 18          time + = 1 ;
 19      }
 20      cout << time;
 21      return  0 ;
 22 }

 

Guess you like

Origin www.cnblogs.com/apprendre-10-28/p/12694185.html