Skip to content

Commit f7f98da

Browse files
Update integer_to_roman.cpp
1 parent 180faf0 commit f7f98da

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Strings/integer_to_roman.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ typedef long long int ll;
99
string int_to_roman(ll n)
1010
{
1111
string str="";
12+
// store all base values in vector in descending order
1213
vector <int > v ={1000,900,500,400,100,90,50,40,10,9,5,4,1};
14+
15+
// store roman numeral equivalent of each base value using map
1316
map <int, string> m ={{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"}};
17+
18+
//now find the largest base value
19+
//dividing the number and repeat the symbol accordingly
1420

1521
for(auto x: v)
1622
{
@@ -19,7 +25,7 @@ string int_to_roman(ll n)
1925
int i= n/x;
2026
n= n%x;
2127
while(i--)
22-
str+= m[x];
28+
str+= m[x]; //repeating the correspoding roman symbol
2329
}
2430
else
2531
return str;

0 commit comments

Comments
 (0)