Prog001v1
Home ] Up ]

 

//file   Prog001v1.cpp
//author AOU
#include <iostream>
using namespace std;

/*
input n1, n2
output the longest maxCycleLength
*/

/*
Algorithm1: to display all the numbers and count
  get a value for n
  display n
  cycleLength = 1
  do while n > 1
    if n is even then 
      n = n/2
    else
      n = 3*n + 1
    end if

    display n
    cycleLength++
    end while

  display cycleLength
*/

void main (void)
  {
  int n;
  int cycleLength;

  cin >> n;
  cout << n << ' ';
  cycleLength = 1;
  while (n > 1)
    {
    if (n%2 == 0)
        n = n/2;
      else
        n = 3*n + 1;

    cout << n << ' ';
    cycleLength++;
    }

  cout << endl;
  cout << "cycleLength = " << cycleLength << endl;
  }

/*
22
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
cycleLength = 16
Press any key to continue
*/