Skip to content

Commit 91f3094

Browse files
Create integer_to_roman.cpp
1 parent 7299d00 commit 91f3094

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Strings/integer_to_roman.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Convert an integer number n to roman number.
2+
3+
#include<bits/stdc++.h>
4+
#define fl(i,a,b) for(i=a;i<b;i++)
5+
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
6+
using namespace std;
7+
typedef long long int ll;
8+
9+
string int_to_roman(ll n)
10+
{
11+
string str="";
12+
vector <int > v ={1000,900,500,400,100,90,50,40,10,9,5,4,1};
13+
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"}};
14+
15+
for(auto x: v)
16+
{
17+
if(n>0)
18+
{
19+
int i= n/x;
20+
n= n%x;
21+
while(i--)
22+
str+= m[x];
23+
}
24+
else
25+
return str;
26+
}
27+
return str;
28+
}
29+
30+
int main()
31+
{
32+
fast;
33+
34+
// #ifndef ONLINE_JUDGE
35+
// freopen("input.txt","r",stdin);
36+
// freopen("output.txt","w",stdout);
37+
// #endif
38+
39+
ll n; cin>>n;
40+
cout<<int_to_roman(n);
41+
42+
return 0;
43+
44+
}

0 commit comments

Comments
 (0)