끄적끄적 코딩
article thumbnail
Published 2019. 9. 13. 17:16
[C++] 백준 1057번 토너먼트 알고리즘

N명이 토너먼트를 할 때 A와 B는 몇번째에 만날 수 있는지 출력하는 문제입니다.
문제는 A와 B는 항상 이기는 경우에 대해서만 생각합니다.

x/2 + x%2를 하여서 매 경기마다의 위치를 파악할 수 있었습니다.

 

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main(int argc, char* argv[])
{
	int n;
	int a, b;
	int count = 0;

	cin >> n;
	cin >> a >> b;

	while (a != b) {
		a = a / 2 + a % 2;
		b = b / 2 + b % 2;
		++count;
	}

	cout << count << endl;

	return 0;
}

'알고리즘' 카테고리의 다른 글

[C++] 백준 7562번 나이트의 이동  (0) 2019.09.13
[C++] 백준 2468번 안전 영역  (0) 2019.09.13
[C++] 백준 1977번 완전제곱수  (0) 2019.09.13
[C++] 백준 1076번 저항  (0) 2019.09.13
[C++] 백준 2010번 플러그  (0) 2019.09.13

검색 태그