forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_Number_to_Integer.cpp
More file actions
50 lines (40 loc) · 935 Bytes
/
Roman_Number_to_Integer.cpp
File metadata and controls
50 lines (40 loc) · 935 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
//{ Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution {
public:
int romanToDecimal(string &str) {
// code here
unordered_map<char, int> map;
map['I'] = 1;
map['V'] = 5;
map['X'] = 10;
map['L'] = 50;
map['C'] = 100;
map['D'] = 500;
map['M'] = 1000;
int res = 0;
for(int i=0; i<str.length(); i++){
if(map[str[i]] < map[str[i+1]])
res += - map[str[i]];
else
res += map[str[i]];
}
return res;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
Solution ob;
cout << ob.romanToDecimal(s) << endl;
}
}
// } Driver Code Ends