※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 12518번 문제인 Centauri Prime (Small2)이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/12518
12518번: Centauri Prime (Small2)
Back in the old days before the creation of the mighty Centauri Republic, the planet Centauri Prime was split into several independent kingdoms. The kingdom of Mollaristan was ruled by king Loatold, while the kingdom of Auritania was under the rule of quee
www.acmicpc.net
(대소문자 구분 없이) 주어지는 나라의 이름의 마지막 문자가 'y'이면 "(나라이름) is ruled by nobody."를, 모음(a, e, i, o 또는 u)이면 "(나라이름) is ruled by a queen."을, 그렇지않으면(=자음이면) "(나라이름) is ruled by a king."을 출력하는 문제이다. 나라의 이름의 마지막 문자를 보고 각 문자가 y인지 모음인지 아니면 자음인지를 판단해 정해진 형식에 맞춰 출력해 문제를 해결해주자.
글쓴이는 tolower을 이용해 대소문자를 모두 소문자로 통일해 판단하는 것으로 구현을 간단하게 하였다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
int T;
string s;
bool isvowel[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
isvowel['a'] = isvowel['e'] = isvowel['i'] = isvowel['o'] = isvowel['u'] = 1;
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> s;
cout << "Case #" << t << ": " << s << " is ruled by ";
if (tolower(s.back()) == 'y') {
cout << "nobody.\n";
}
else if (isvowel[tolower(s.back())]) cout << "a queen.\n";
else cout << "a king.\n";
}
}
'BOJ' 카테고리의 다른 글
[BOJ 5698 // C++] Tautogram (0) | 2023.02.21 |
---|---|
[BOJ 6550 // C++] 부분 문자열 (0) | 2023.02.21 |
[BOJ 24039 // C++] 2021은 무엇이 특별할까? (0) | 2023.02.21 |
[BOJ 12871 // C++] 무한 문자열 (0) | 2023.02.21 |
[BOJ 9694 // C++] 무엇을 아느냐가 아니라 누구를 아느냐가 문제다 (0) | 2023.02.21 |