kar7mp5

[Baekjoon] 16953 A --> B (C++ 풀이) 본문

Problem Solving/Baekjoon

[Baekjoon] 16953 A --> B (C++ 풀이)

kar7mp5 2024. 6. 5. 15:08
728x90

https://www.acmicpc.net/problem/16953

#include <iostream>
using namespace std;

using ll = long long;

ll A, B;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> A >> B;

    int cnt = 1;
    while (true) {
        if (A == B) {
            cout << cnt;
            break;
        }
        if (A > B) {
            cout << -1;
            break;
        }

        if (B % 2 == 0) {
            B /= 2;
            ++cnt;
        } else if (B % 10 == 1) {
            B /= 10;
            ++cnt;
        } else {
            cout << -1;
            break;
        }
    }

    return 0;
}
728x90