Created
February 6, 2025 10:47
-
-
Save Arnau478/f01f3cc1fccb02105cc217456ff3c3d8 to your computer and use it in GitHub Desktop.
Roman numerals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
using ll = long long; | |
using str = string; | |
using pii = pair<int, int>; | |
using vi = vector<int>; | |
#define dbg(v) cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl; | |
#define rep(i, a, n) for(int i = a; i < n; i++) | |
#define size(a) (int)a.size() | |
#define len(a) (int)a.length() | |
#define nl '\n' | |
#define order(a) sort(a.begin(), a.end()) | |
#define rorder(a) sort(a.rbegin(), a.rend()) | |
int main() { | |
ios::sync_with_stdio(0); | |
cin.tie(0); | |
int t; | |
cin >> t; | |
while(t--) { | |
int n; | |
cin >> n; | |
while(n > 0) { | |
if(n >= 1000) { | |
cout << 'M'; | |
n -= 1000; | |
} else if(n >= 900) { | |
cout << "CM"; | |
n -= 900; | |
} else if(n >= 500) { | |
cout << 'D'; | |
n -= 500; | |
} else if(n >= 400) { | |
cout << "CD"; | |
n -= 400; | |
} else if(n >= 100) { | |
cout << 'C'; | |
n -= 100; | |
} else if(n >= 90) { | |
cout << "XC"; | |
n -= 90; | |
} else if(n >= 50) { | |
cout << 'L'; | |
n -= 50; | |
} else if(n >= 40) { | |
cout << "XL"; | |
n -= 40; | |
} else if(n >= 10) { | |
cout << 'X'; | |
n -= 10; | |
} else if(n == 9) { | |
cout << "IX"; | |
n -= 9; | |
} else if(n >= 5) { | |
cout << 'V'; | |
n -= 5; | |
} else if(n == 4) { | |
cout << "IV"; | |
n -= 4; | |
} else if(n >= 1) { | |
cout << 'I'; | |
n -= 1; | |
} | |
} | |
cout << nl; | |
} | |
} | |
/* | |
int romanToInt(string s) { | |
unordered_map<char, int> m; | |
m['I'] = 1; | |
m['V'] = 5; | |
m['X'] = 10; | |
m['L'] = 50; | |
m['C'] = 100; | |
m['D'] = 500; | |
m['M'] = 1000; | |
int ans = 0; | |
for(int i = 0; i < s.length(); i++){ | |
if(m[s[i]] < m[s[i+1]]){ | |
ans -= m[s[i]]; | |
} | |
else{ | |
ans += m[s[i]]; | |
} | |
} | |
return ans; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment