欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

探讨罗马数字与阿拉伯数字的比较研究

最编程 2024-07-29 17:39:19
...
using namespace std;
class Solution {
public:
int romanToInt(string s) {
int l=s.length();
int pre=0,now=0,ans=0;
for(int i=0;i<l;i++){
if(s[i]'I'){ now=1;
if(i+1<l&&s[i+1]
'V') { now=4; i++; }
else if(i+1<l&&s[i+1]'X') { now=9; i++; }
}
else if(s[i]
'X') { now=10;
if(i+1<l&&s[i+1]'L') { now=40; i++; }
else if(i+1<l&&s[i+1]
'C') { now=90; i++; }
}
else if(s[i]'C') { now=100;
if(i+1<l&&s[i+1]
'D') { now=400; i++; }
else if(i+1<l&&s[i+1]'M') { now=900; i++; }
}
else if(s[i]
'M') { now=1000; }
else if(s[i]'V') { now=5; }
else if(s[i]
'L') { now=50; }
else if(s[i]=='D') { now=500; }

推荐阅读