문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/12985
코딩테스트 연습 - 예상 대진표
△△ 게임대회가 개최되었습니다. 이 대회는 N명이 참가하고, 토너먼트 형식으로 진행됩니다. N명의 참가자는 각각 1부터 N번을 차례대로 배정받습니다. 그리고, 1번↔2번, 3번↔4번, ... , N-1번↔N
programmers.co.kr
대진표가 층마다 2배씩 줄어드는 것을 생각해보면 풀기 쉬운 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int solution(int n, int a, int b) | |
{ | |
a--, b--; | |
for (int round = 1; round <= log2(n); round++) | |
{ | |
a /= 2; | |
b /= 2; | |
if (a == b) | |
{ | |
return round; | |
} | |
} | |
} | |
int main(void) | |
{ | |
int N = 8; | |
int A = 4; | |
int B = 7; | |
cout << solution(N, A, B) << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 폰켓몬 (0) | 2021.09.30 |
---|---|
[Programmers] 단어 퍼즐 (0) | 2021.09.30 |
[Programmers] 짝지어 제거하기 (0) | 2021.09.30 |
[Programmers] 모두 0으로 만들기 (0) | 2021.09.29 |
[Programmers 위클리 챌린지 8주차] 최소직사각형 (0) | 2021.09.27 |