-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.cpp
More file actions
115 lines (101 loc) · 1.88 KB
/
Copy path12.cpp
File metadata and controls
115 lines (101 loc) · 1.88 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<bits/stdc++.h>
#include<String.h>
#include<iostream>
using namespace std;
int romanToInt(string s) {
int result=0;
const char* p=s.c_str();
int count=0;
//千位
while(*p!='\0'&&*p=='M'){
p++;//千位循环结束后p指向百位。
count++;
}
result+=1000*count;
count=0;
//百位
if(*p=='C'){
while(*p!='\0'&&*p=='C'){
p++;
count++;
}
if(count==1&&*p=='M'){
p++;
count=9;
}
else if(count==1&&*p=='D'){
p++;
count=4;
}
}else if(*p=='D') {
p++;
count=5;
while(*p!='\0'&&*p=='C'){
p++;
count++;
}
}else{
count=0;
}
result+=count*100;
count=0;
//十位
if(*p=='X'){
while(*p!='\0'&&*p=='X'){
p++;
count++;
}
if(count==1&&*p=='C'){
p++;
count=9;
}
else if(count==1&&*p=='L'){
p++;
count=4;
}
}else if(*p=='L') {
p++;
count=5;
while(*p!='\0'&&*p=='X'){
p++;
count++;
}
}else{
count=0;
}
result+=count*10;
count=0;
//个位
if(*p=='I'){
while(*p!='\0'&&*p=='I'){
p++;
count++;
}
if(count==1&&*p=='X'){
p++;
count=9;
}
else if(count==1&&*p=='V'){
p++;
count=4;
}
}else if(*p=='V') {
p++;
count=5;
while(*p!='\0'&&*p=='I'){
p++;
count++;
}
}else{
count=0;
}
result+=count;
return result;
}
int main()
{
string s;
cin>>s;
cout<<romanToInt(s)<<endl;
cout<<"hello";
}