-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.cpp
More file actions
49 lines (45 loc) · 1.18 KB
/
13.cpp
File metadata and controls
49 lines (45 loc) · 1.18 KB
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
/*************************************************************************
> File Name: 13.cpp
> Author: Alan
> Mail: [email protected]
> Created Time: Fri 13 Nov 2015 10:28:26 AM CST
> Problem Name: Roman to Integer My Submissions Question
> Difficulty: Easy
> Description:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
************************************************************************/
#include<iostream>
using namespace std;
class Solution
{
public:
int romanToInt(string s)
{
int num[256] = { 0 };
int result = 0;
num['I'] = 1;
num['V'] = 5;
num['X'] = 10;
num['L'] = 50;
num['C'] = 100;
num['D'] = 500;
num['M'] = 1000;
int i = 0;
while (i < s.size())
{
if (num[s[i]] < num[s[i + 1]])
result -= num[s[i]];
else
result += num[s[i]];
i++;
}
return result;
}
};
int main()
{
string str = "DCXL";
Solution sol = Solution();
cout << "The value of " << str << " is " << sol.romanToInt(str) << endl;
}