프로그래머스 - 예상 대진표

well-life-gm·2021년 11월 26일
0

프로그래머스

목록 보기
70/125

프로그래머스 - 예상 대진표

모든 홀수는 짝수로 바꿔주고, 우측으로 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;
}

결과

profile
내가 보려고 만든 블로그

0개의 댓글