Advertisement
tarek_ullah

three_n_plus_one

Mar 2nd, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. /*author : H. M. Tarek Ullah
  2.   date : .0.02.15
  3.   problem : 3n + 1 of UVA
  4.  there is some  mistake as i read the problem incorrectly . need to redo this program.
  5.  */  
  6. #include<iostream>
  7. #include<vector>
  8.  
  9. using namespace std;
  10.  
  11. bool odd_even(int num){
  12.     if (num % 2 == 0)
  13.         return true;
  14.     else
  15.         return false;
  16. }
  17.  
  18. int main()
  19. {
  20.     int starting_point = 0;
  21.     int ending_point = 0;
  22.     int copy_ending = 0;
  23.     unsigned long int  vec_size= 0;
  24.     cin >> starting_point;
  25.     cin >> ending_point;
  26.     copy_ending = ending_point;
  27.  
  28.     vector<int> vec;
  29.  
  30.     while (ending_point != 1){
  31.         if (odd_even(ending_point)){
  32.             ending_point = ending_point / 2;
  33.             if (ending_point < starting_point){
  34.                 vec.push_back(ending_point);
  35.             }
  36.  
  37.         }
  38.         else{
  39.             ending_point = (ending_point * 3) + 1;
  40.             if (ending_point < starting_point){
  41.                 vec.push_back(ending_point);
  42.             }
  43.         }
  44.     }
  45.  
  46.     vec_size = vec.size() ;
  47.  
  48.     cout << starting_point<<" "<< copy_ending<<" "<< vec_size << endl;
  49.  
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement