1. 문제 풀이
문제 유형은 문자열 + 구현입니다. 로마 숫자를 아라비아 숫자로 변환하는 방법과 아라비아 숫자를 로마숫자로 변환하는 로직을 구현해야 합니다. 우선 문제에서 주어진 아라비아 숫자를 내림차순으로 정렬하고 이에 맞게끔 로마 숫자들을 관리합니다. 그다음, 로마 숫자를 1개 또는 2개 받아와 아라비아 숫자로 변환이 가능한지 확인하는 방법을 사용했습니다. 아라비아 숫자를 로마 숫자로 바꾸는 과정도 이와 유사합니다.
2. 코드
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pi; typedef pair<ll, ll> pl;
typedef tuple<int, int, int> ti; typedef tuple<ll, ll, ll> tl; typedef vector<int> vi; typedef vector<ll> vl;
typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<ti> vti; typedef vector<tl> vtl;
typedef vector<string> vs; typedef vector<bool> vb; typedef queue<int> qi; typedef queue<ll> ql;
typedef queue<pi> qpi; typedef queue<pl> qpl; typedef queue<ti> qti; typedef queue<tl> qtl;
#define fastio(x, y) cin.tie((x))->sync_with_stdio((y))
#define X first
#define Y second
#define pb push_back
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const char nl = '\n';
vi arabia_numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
vs roma_numbers = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
// 아라비아 숫자 -> 로마 숫자 변환
string convert_r_num(int number) {
string ret{};
while(number) {
for(int i = 0; i < sz(arabia_numbers); i++) {
if(number < arabia_numbers[i]) continue;
number -= arabia_numbers[i];
ret += roma_numbers[i];
break;
}
}
return ret;
}
// 로마 숫자 -> 아라비아 숫자 변환
int convert_a_num(const string &number) {
int n = sz(number);
bool flag{};
int ans{}, idx{};
while(idx < n - 1) {
string cur = string(1, number[idx]);
cur.push_back(number[idx + 1]);
int fdx = find(all(roma_numbers), cur) - roma_numbers.begin();
if(fdx != sz(arabia_numbers)) {
ans += arabia_numbers[fdx];
idx += 2;
continue;
}
cur.pop_back();
fdx = find(all(roma_numbers), cur) - roma_numbers.begin();
ans += arabia_numbers[fdx];
idx++;
}
if(idx == n - 1) {
int fdx = find(all(roma_numbers), string(1, number[n - 1])) - roma_numbers.begin();
ans += arabia_numbers[fdx];
}
return ans;
}
int main() {
fastio(nullptr, false);
string a, b;
cin >> a >> b;
int ans = convert_a_num(a) + convert_a_num(b);
cout << ans << nl << convert_r_num(ans);
}
음 이 코드는 제가 봐도 깔끔하지 않아서 별로 채택하고 싶진 않네요.
3. 제출 결과
'알고리즘 > 백준' 카테고리의 다른 글
백준 1749번 점수따먹기[C++] (0) | 2025.02.12 |
---|---|
백준 1949번 우수 마을[C++] (0) | 2025.02.10 |
백준 10827번 a^b[Java] (0) | 2025.02.05 |
백준 20055번 컨베이어 벨트 위의 로봇[C++] (0) | 2025.02.03 |
백준 17141번 연구소 2[C++] (0) | 2025.02.02 |