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

 

이번에 볼 문제는 백준 4459번 문제인 Always Follow the Rules in Zombieland이다.
문제는 아래 링크를 확인하자.

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

 

4459번: Always Follow the Rules in Zombieland

Input will begin with an integer q, 0 < q ≤ 50, on its own line signifying the number of quotes. On the following lines will be the quotes, one per line. No quote will be greater than 65 characters. The first quote will be rule 1, the second quote rule 2

www.acmicpc.net

주어진 1번부터 Q번까지의 quote들을 입력받아 저장해두고, 해당 범위의 수를 입력받으면 대응되는 quote를, 그렇지 않다면 "No such rule"을 출력해 문제를 해결하자.

 

getline을 이용하면 주어지는 입력을 손쉽게 받을 수 있다.

 

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

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

int Q, R;
string quotes[51];
string s;

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

	cin >> Q;
	for (int i = 0; i <= Q; i++) getline(cin, quotes[i]);
	cin >> R;
	while (R--) {
		int q; cin >> q;
		if (q<1 || q>Q) cout << "Rule " << q << ": " << "No such rule\n";
		else cout << "Rule " << q << ": " << quotes[q] << '\n';
	}
}
728x90

+ Recent posts