fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n ;
  7. cin>> n;
  8. vector<int>ans;
  9. while(true){
  10. if(n == 1){
  11. ans.push_back(n);
  12. break;
  13. }
  14. if(n%2 == 1){
  15. ans.push_back(n);
  16. n = n*3 +1;
  17.  
  18. }else{
  19. ans.push_back(n);
  20. n = n/2;
  21.  
  22. }
  23. }
  24.  
  25. for(auto it:ans){
  26. cout<<it<<" ";
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5476KB
stdin
3
stdout
3 10 5 16 8 4 2 1