Skip to content

Commit b28a091

Browse files
authored
Merge pull request #33 from aashishchachan/different
Given an integer less than 10000, return the number in its Roman type
2 parents d0a6994 + b72faa2 commit b28a091

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

CP-Programming/integerToRoman.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Given an integer less than 10000, return the number in its Roman type.
2+
// Symbol Value
3+
// I 1
4+
// V 5
5+
// X 10
6+
// L 50
7+
// C 100
8+
// D 500
9+
// M 1000
10+
// R for others (unkonwn)
11+
12+
#include <bits/stdc++.h>
13+
using namespace std;
14+
15+
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL);
16+
#define external_io() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
17+
18+
19+
void allot(string &l, string &m, string &h, int n){
20+
if(n==1){
21+
l="I";
22+
m="V";
23+
h="X";
24+
}
25+
if(n==10){
26+
l="X";
27+
m="L";
28+
h="C";
29+
}
30+
if(n==100){
31+
l="C";
32+
m="D";
33+
h="M";
34+
}
35+
if(n==1000){
36+
l="M";
37+
m="R"; //don't know
38+
h="R"; //don't know
39+
}
40+
return;
41+
}
42+
43+
string intToRoman(int num) {
44+
string sol="";
45+
int c=num;
46+
int n=10000;
47+
string l, m, h;
48+
while(n>1){
49+
n/=10;
50+
allot(l,m,h,n);
51+
c= num/n;
52+
if(c==0);
53+
else if(c<4){
54+
int t=c;
55+
while(t--){
56+
sol+=l;
57+
}
58+
}
59+
else if(c==4){
60+
sol+=l;
61+
sol+=m;
62+
}
63+
else if(c==5){
64+
sol+=m;
65+
}
66+
else if(c<9) {
67+
sol+=m;
68+
int t=c%5;
69+
while(t--){
70+
sol+=l;
71+
}
72+
}
73+
else if (c==9) {
74+
sol+=l;
75+
sol+=h;
76+
}
77+
num=num%n;
78+
}
79+
return sol;
80+
}
81+
82+
int main(){
83+
fast_cin();
84+
int n;
85+
cin>>n;
86+
string sol = intToRoman(n);
87+
cout << sol;
88+
return 0;
89+
}

0 commit comments

Comments
 (0)