はまやんはまやんはまやん

hamayanhamayan's blog

Collatz [yukicoder 887]

https://yukicoder.me/problems/no/887

解説

https://yukicoder.me/submissions/382000

制約を見てみると、シミュレートできそうな雰囲気がある。
400回までが上限なので、ぶん回す。

int n0;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> n0;

    int i1 = -1, nmax = n0;
    int n = n0;
    rep(i, 0, 400)
    {
        if (n == 1 and i1 < 0) {
            i1 = i;
            break;
        }

        chmax(nmax, n);

        if (n % 2 == 0)
            n = n / 2;
        else
            n = 3 * n + 1;
    }
    cout << i1 << endl;
    cout << nmax << endl;
}