Tuesday, December 26, 2023

CSES :: Introductory Problems :: Weird Algorithm

Problem : Please find the problem here.

Explanation : 

Code : 

#include <bits/stdc++.h>
 
using namespace std;
 
void solve() {
    int n;
    cin >> n;
 
    cout << n << ' ';
    while (n != 1)
    {
        n = n & 1 ? n / 2 : n * 3 + 1;
        cout << n << ' ';
    }
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    solve();
 
    return 0;
}

No comments:

Post a Comment