-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman_to_integer.cpp
95 lines (91 loc) · 2.23 KB
/
roman_to_integer.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<string>
using namespace std;
// Symbol Value
// I 1
// V 5
// X 10
// L 50
// C 100
// D 500
// M 1000
// I can be placed before V (5) and X (10) to make 4 and 9.
// X can be placed before L (50) and C (100) to make 40 and 90.
// C can be placed before D (500) and M (1000) to make 400 and 900.
int main(){
string roman;
cin>>roman;
int number = 0;
for(int i = 0; i < roman.length(); i++){
if(roman[i] == 'I'){
if((roman[i+1] == 'X') || (roman[i+1] =='V')){
number += 0;
}
else{
number += 1;
}
}
if(roman[i] == 'V'){
if(roman[i-1] == 'I'){
number += 4;
}
else{
number += 5;
}
}
if(roman[i] == 'X'){
if(roman[i-1] == 'I'){
number += 9;
}
else{
if((roman[i+1] == 'L') || (roman[i+1] == 'C')){
number += 0;
}
else{
number += 10;
}
}
}
if(roman[i] == 'L'){
if(roman[i-1] == 'X'){
number += 40;
}
else{
number += 50;
}
}
if(roman[i] == 'C'){
if(roman[i-1] == 'X'){
number += 90;
}
else{
if((roman[i+1] == 'M') || (roman[i+1] == 'D')){
number += 0;
}
else{
number += 100;
}
}
}
if(roman[i] == 'D'){
if(roman[i-1] == 'C'){
number += 400;
}
else{
number += 500;
}
}
if(roman[i] == 'M'){
if(roman[i-1] == 'C'){
number += 900;
}
else{
number += 1000;
}
}
}
cout<<number;
return 0;
}
// done
// https://leetcode.com/problems/roman-to-integer/