모든 홀수는 짝수로 바꿔주고, 우측으로 1칸 쉬프트 해주면 된다. (그냥 정수형 나눗셈해도 된다)
Stage 1 : 1 2 3 4 -> 2 2 4 4
Stage 2 : 1 2 -> 2 2
a와 b가 같으면 끝내면 된다.
코드는 아래와 같다.
#include <iostream>
using namespace std;
int solution(int n, int a, int b)
{
int answer = 0;
while(1) {
++answer;
if(a & 1) a++;
if(b & 1) b++;
a >>= 1;
b >>= 1;
if(a == b)
break;
}
return answer;
}