```cpp
//第一种
#include <iostream>
using namespace std;
int main()
{
int n, a[1002];
cin >> n;
int total = 0;
a[0] = n;
if (n == 1)cout << a[0];
else {
for (int i = 1; i < 100; i++)
{
if (n % 2 == 0)n = n / 2;
else n = n * 3 + 1;
a[i] = n;
if (n == 1)break;
total = i;
}
for (int j = total + 1; j >= 0; j--)
{
cout << a[j] << " ";
}
}
return 0;
}
//第二种
#include <iostream>
using namespace std;
int main()
{
int n=0, a[1002];
cin >> n;
int total = 0;
a[0] = n;
if (n == 1)cout << a[0];
else {
for (int i = 1;n!=1; i++)
{
if (n % 2 == 0)n = n / 2;
else n = n * 3 + 1;
a[i] = n;
total = i;
}
for (int j = total; j >= 0; j--)
{
cout << a[j] << " ";
}
}
return 0;
}