mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Implement integer to Roman numeral conversion
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
class Solution {
|
||||
public:
|
||||
string intToRoman(int num) {
|
||||
const vector<pair<int,string>> valueSymbols = {
|
||||
{1000,"M"}, {900,"CM"}, {500, "D"}, {400,"CD"},{100,"C"},{90,"XC"},{50,"L"}, {40,"XL"},{10,"X"},{9,"IX"},{5,"V"},{4,"IV"},{1,"I"}
|
||||
|
||||
};
|
||||
string res;
|
||||
for(auto const &[value,symbol]: valueSymbols){
|
||||
if(num == 0)
|
||||
break;
|
||||
while(num>=value){
|
||||
res+=symbol;
|
||||
num-=value;
|
||||
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user