※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 5370번 문제인 Which Way이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/5370 

 

5370번: Which Way

You are trapped in Jabba’s Palace. You have a coded map that describes the way out from your current location. The map contains a sequence of positive integers. Each integer corresponds to one of three directions (left, straight, right). To determine the

www.acmicpc.net

주어진 수를 leading zero 없는 이진수로 표현할 때 각 자리에 0과 1중 어느 것이 더 많은지(혹은 같은지)를 판별하는 문제이다.

 

주어진 수를 2로 나누어가면서 일의 자리에 등장하는 수를 반복적으로 확인해 문제를 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int N;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	while (cin >> N) {
		if (N) {
			int L = 0, R = 0;
			while (N) {
				if (N & 1) R++;
				else L++;
				N >>= 1;
			}

			if (L > R) cout << "left\n";
			else if (L < R) cout << "right\n";
			else cout << "straight\n";
		}
		else cout << "left\n";
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 6765 // C++] Icon Scaling  (0) 2023.08.22
[BOJ 3447 // C++] 버그왕  (0) 2023.08.21
[BOJ 2897 // C++] 몬스터 트럭  (0) 2023.08.21
[BOJ 15823 // C++] 카드 팩 구매하기  (0) 2023.08.21
[BOJ 15825 // C++] System Call  (0) 2023.08.20

+ Recent posts