백준 16953번 A → B

김두현·2022년 11월 30일
1

백준

목록 보기
32/133
post-thumbnail

🔒[문제 url]

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


🪄전체 코드

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> // cpp
using namespace std;

int a, b;

void INPUT()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> a >> b;
}



void SOLVE()
{
    // Requirement : Output (min value + 1)
    int ans = 1;

    /* I used Greedy Algorithm.Tried to change B to A.
    * Why B to A?
    * => It can be hard to find what calc is useful to make A to B.
    */
    while (a != b)
    {
        // B should not smaller than A
        // B should do calculate % 10 or % 2
        if(b < a || (b % 10 != 1 && b % 2 == 1))
        {
            ans = -1;
            break;
        }

        // Only Two Possiblity
        if (b % 10 == 1) b /= 10;
        else if (b % 2 == 0) b /= 2;
        ans++;
    }
    cout << ans;
}

/* You can also use BFS to solve this problem. */
int main()
{
    INPUT();

    SOLVE();
}

🥇문제 후기

GOLD5 미만 난이도는 알고리즘 및 풀이 설명을 주석으로 대체합니다.
주석을 참고해주세요.


💕오류 지적 및 피드백은 언제든 환영입니다. 복제시 출처 남겨주세요!💕
💕좋아요와 댓글은 큰 힘이 됩니다.💕
profile
I AM WHO I AM

0개의 댓글