본문 바로가기
이론/코딩테스트

[백준] 16953번 A -> B (그리디 알고리즘)

by 퇴근후개발 2022. 8. 7.
반응형

-문제

 

-코드

#include<stdio.h>
using namespace std;

int main()
{
	int a, b, cnt=1;
	scanf("%d %d", &a, &b);
	while (a < b)
	{
		if (b % 2 == 0)
		{
			b = b / 2;
			cnt++;
		}
		else if ((b - 1) % 10 == 0)
		{
			b = (b - 1) / 10;
			cnt++;
		}
		else
		{
			printf("%d", -1);
			return 0;
		}
	}

	if(a == b) 	printf("%d", cnt);
	else printf("%d", -1);
	return 0;
}
반응형