※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 21235번 문제인 Year of the Cow이다.
문제는 아래 링크를 확인하자.
www.acmicpc.net/problem/21235
21235번: Year of the Cow
Farmer John's cows are excited to learn that Chinese New Year was recently celebrated, ushering in the year of the Ox, always a bovine favorite. As we know, the zodiac animals for Chinese calendar years follow a 12-year cycle: Ox, Tiger, Rabbit, Dragon, Sn
www.acmicpc.net
문제를 간단히 요약하면 다음과 같다.
1) 첫 줄에 앞으로 나올 문장의 수 N을 입력으로 준다.
2) 다음 N줄에는 "(소1) born in (next/previous) (12간지) year from (소2)" 와 같은 문장이 주어진다.
2-1) 소1은 (Bessie 또는 이미 앞선 문장들에서 이름이 등장한 적이 있는 소 이름)이 아닌 이름이 주어진다.
2-2) 소2는 (Bessie 또는 이미 앞선 문장들에서 이름이 등장한 적이 있는 소 이름)인 이름이 주어진다.
2-3) Elsie라는 이름을 가진 소는 반드시 등장한다. 또한, Bessie는 소띠(Ox)이다.
3) Bessie와 Elsie 사이에 생년이 얼마나 차이가 날지를 계산하여 출력한다.
이 문제는 주어진 조건을 그대로 구현하기만 하면 되는 간단한 문제이다.
단, 위에서 주어지는 문장으로는 절대 생년이 같은 소가 주어질 수가 없으므로, 같은 간지에 태어난 소들의 조건을 세울 때 약간의 주의를 기울일 필요가 있다.
문제의 입력이 문자열로 주어지므로, std::map을 이용하여 문제를 해결하였다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, int> mp;
int arr[101];
int animal[101];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
mp.insert({ "Bessie",0 });
for (int i = 1;i <= N;i++) {
string s; cin >> s;
mp.insert({ s,i });
cin >> s >> s >> s;
bool next = 0;
if (s[0] == 'n') next = 1;
cin >> s;
if (s == "Ox") animal[i] = 0;
else if (s == "Tiger") animal[i] = 1;
else if (s == "Rabbit") animal[i] = 2;
else if (s == "Dragon") animal[i] = 3;
else if (s == "Snake") animal[i] = 4;
else if (s == "Horse") animal[i] = 5;
else if (s == "Goat") animal[i] = 6;
else if (s == "Monkey") animal[i] = 7;
else if (s == "Rooster") animal[i] = 8;
else if (s == "Dog") animal[i] = 9;
else if (s == "Pig") animal[i] = 10;
else if (s == "Rat") animal[i] = 11;
cin >> s >> s >> s;
int idx = mp[s];
arr[i] = animal[i] - animal[idx];
if (next) {
if (arr[i] <= 0) arr[i] += 12;
}
else {
if (arr[i] >= 0) arr[i] -= 12;
}
arr[i] += arr[idx];
}
cout << abs(arr[mp["Elsie"]]);
}
'BOJ' 카테고리의 다른 글
[BOJ 2742 // C++] 기찍 N (0) | 2021.05.01 |
---|---|
[BOJ 21236 // C++] Comfortable Cows (0) | 2021.04.30 |
[BOJ 2463 // C++] 비용 (0) | 2021.04.28 |
[BOJ 15285 // C++] Connections (0) | 2021.04.27 |
[BOJ 15517 // C++] Array Manipulation at Moloco (Hard) (0) | 2021.04.26 |