-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_to_integer.cpp
More file actions
56 lines (40 loc) · 847 Bytes
/
Roman_to_integer.cpp
File metadata and controls
56 lines (40 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include<string>
using namespace std;
int num(char c) {
if(c == 'I')
return 1;
else if(c=='V')
return 5;
else if(c=='X')
return 10;
else if(c=='L')
return 50;
else if(c=='C')
return 100;
else if(c=='D')
return 500;
else if(c=='M')
return 1000;
}
int roman_to_int(string s) {
int index = 0;
int sum = 0;
while(index < s.size()-1) {
if(num(s[index]) < num(s[index+1])) {
sum -= num(s[index]);
}
else {
sum += num(s[index]);
}
index ++;
}
sum += num(s[s.size()-1]);
return sum;
}
int main() {
string s;
cout << "Enter String" << endl;
cin >> s;
cout << roman_to_int(s);
}