码迷,mamicode.com
首页 > 其他好文 > 详细

Roman to Integer

时间:2014-08-01 23:01:02      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:blog   io   for   问题   ar   div   amp   size   

问题:罗马数字变为整数

class Solution {
public:
    int romanToInt(string s) {
         char c[10][10][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},{"0","X","XX","XXX","XL","L","LX"
        ,"LXX","LXXX","XC"},{"0","C","CC","CCC","CD","D",
              "DC","DCC","DCCC","CM"},{"0","M","MM","MMM"}};
         int num=0,i;
         for(i=0;i<s.size();i++)
         {
             if(s[i]==‘I‘) num+=1;
             if(s[i]==‘V‘) 
             {
                if(i!=0 && s[i-1]==‘I‘) num+=3;
                else num+=5;
             }
             if(s[i]==‘X‘)
             {
                 if(i!=0 && s[i-1]==‘I‘) num+=8;
                 else num+=10;
             }
             if(s[i]==‘L‘)
             {
                 if(i!=0 && s[i-1]==‘X‘) num+=30;
                 else num+=50;
             }
             if(s[i]==‘C‘)
             {
                 if(i!=0 && s[i-1]==‘X‘) num+=80;
                 else num+=100;
             }
             if(s[i]==‘D‘)
             {
                 if(i!=0 && s[i-1]==‘C‘) num+=300;
                 else num+=500;
             }
             if(s[i]==‘M‘)  
             {
                 if(i!=0 && s[i-1]==‘C‘) num+=800;
                 else num+=1000;
             }
         }
         return num;
    }
};

  

Roman to Integer,布布扣,bubuko.com

Roman to Integer

标签:blog   io   for   问题   ar   div   amp   size   

原文地址:http://www.cnblogs.com/zsboy/p/3885881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!