Problem Link: Weird Algorithm
Approach
Simulate the Collatz sequence: if is even divide by 2, if odd multiply by 3 and add 1, until .
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n; cin >> n;
while (n != 1) {
cout << n << " ";
n = (n % 2 == 0) ? n / 2 : 3 * n + 1;
}
cout << 1;
}