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

 

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

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

 

27481번: Hotelier

In the only line, output the hotel room’s assignment status, from room $0$ to room $9$. Represent an empty room as ‘0’, and an occupied room as ‘1’, without spaces.

www.acmicpc.net

'L', 'R' 및 한자리 숫자로 이루어진 문자열을 입력받아 해당 문자가 의미하는 바를 직접 시뮬레이션하는 것으로 문제를 해결할 수 있다.

 

정답으로 제출할 문자열을 '0' 10개로 이루어진 "0000000000"과 같이 준비해두고, 각 사건('L','R','(한자리수)')에 대응되는 변화를 위 문자열에 주는 것을 반복해 시뮬레이션 코드를 작성하자.

 

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

#include <iostream>
#include <string>
using namespace std;

int N;
string s;
string ans = "0000000000";

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

	cin >> N >> s;
	for (auto& l : s) {
		if (l == 'L') {
			for (int i = 0; i < 10; i++) {
				if (ans[i] == '0') {
					ans[i] = '1';
					break;
				}
			}
		}
		else if (l == 'R') {
			for (int i = 9; i > -1; i--) {
				if (ans[i] == '0') {
					ans[i] = '1';
					break;
				}
			}
		}
		else ans[l - '0'] = '0';
	}

	cout << ans;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 9772 // C++] Quadrants  (0) 2023.02.13
[BOJ 2292 // C++] 벌집  (0) 2023.02.12
[BOJ 1392 // C++] 노래 악보  (0) 2023.02.11
[BOJ 2839 // C++] 설탕 배달  (0) 2023.02.10
[BOJ 1894 // C++] 4번째 점  (0) 2023.02.10

+ Recent posts