※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 26198번 문제인 Chronogram이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/26198
26198번: Chronogram
The first line of the input contains an integer $T$, i.e. the number of test cases. This is followed by $T$ lines; each line contains one chronogram that contains at most 1000 characters. The text contains only uppercase letters of the English alphabet and
www.acmicpc.net
각 줄에 주어지는 문장에서 로마숫자를 구성하는 알파벳 'I', 'V', 'X', 'L', 'C', 'D' 및 'M'을 발견할 때마다 대응되는 값을 더해 문제를 해결할 수 있다.
로마숫자를 구성하는 알파벳이 아닌 문자의 가중치를 0으로 취급하면 모든 문자의 점수를 합산하는 코드를 작성해 문제를 편하게 해결할 수 있게 된다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
string s;
int val[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
val['I'] = 1, val['V'] = 5, val['X'] = 10, val['L'] = 50, val['C'] = 100, val['D'] = 500, val['M'] = 1000;
getline(cin, s);
while (getline(cin, s)) {
int ans = 0;
for (auto& l : s) {
ans += val[l];
}
cout << ans << '\n';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 24079 // C++] 移動 (Moving) (0) | 2022.12.09 |
---|---|
[BOJ 24606 // C++] Double Password (0) | 2022.12.09 |
[BOJ 2505 // C++] 두 번 뒤집기 (0) | 2022.12.08 |
[BOJ 26209 // C++] Intercepting Information (0) | 2022.12.08 |
[BOJ 2561 // C++] 세 번 뒤집기 (0) | 2022.12.07 |