计蒜客——罗马数字转换成整数

1000ms 65536K 
给定一个罗马数字 ss,将罗马数字转换成整数。

如罗马数字I,II,III,IV,V分别代表数字 1, 2, 3, 4, 51,2,3,4,5。

首先要来了解一下罗马数字表示法,基本字符有 77 个:I、V、X、L、C、D、M,分别表示 11、55、1010、5050、100100、500500、10001000。

在构成数字的时候,有下列规则:

1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:III = 3=3;

2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:VIII = 8=8;XII = 12=12;

3、小的数字,(限于Ⅰ、X和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:IV = 4=4;IX = 9=9;

4、正常使用时,连写的数字重复不得超过三次。

输入格式 
输入一个罗马数字字符串,其对应的数字为 s(1 \leq s \leq 3999)s(1≤s≤3999)。

输出格式 
输出对应的整数 ss。

样例输入 
CXXIII 
样例输出 
123

  1 #include<stdio.h>
  2 //依次往右判断:一个左数,一个右数,一个记录次数。
  3 //如果左数等于右数,则当前位的数加1,次数加1
  4 //能重复出现的只有 
  5 //II III VII VIII
  6 //XX XXX LXX LXXX
  7 //CC CCC DCC DCCC
  8 //MM MMM
  9 //先判断是否有连续的,如果有,得到连续的这一堆的数值。
 10 //如果没有连续,判断前后是否是在同一小组里。
 11 //1、如果是,比较大小,得到值
 12 //2、如果不是,判断是否是相减的情况:IX XC CM
 13 //2.1、如果是相减的,减去得到值
 14 //2.2、如果不是,直接得到值
 15 
 16 int index = 0;
 17 int Count(char* s,char c);//返回字符c在s数组当前位置及后面总共出现次数
 18 int Calculate(char* str);//根据罗马数字计算出对应的整数
 19 int main(void)
 20 {
 21     char str[20];
 22     scanf("%s",str);
 23     printf("%d",Calculate(str));
 24     return 0;
 25 }
 26 int Calculate(char* str)
 27 {
 28     int count = 0,S = 0;
 29     //千位。
 30 
 31     if('M' == str[index])
 32     {
 33         count = Count(str,'M');
 34         S = S+count*1000;
 35         count = 0;//置0
 36     }
 37     //百位
 38     if('C' == str[index] && 'M' == str[index+1])
 39         S = S+900,index+=2;//往后移两位
 40     else if('C' == str[index] && 'D' == str[index+1])
 41     {
 42         S = S + 400,index+=2;
 43     }
 44     else if('C' == str[index])
 45     {
 46         count = Count(str,'C');
 47         S = S + count*100;
 48     }
 49     else if('D' == str[index])
 50     {
 51         index++;
 52         S = S + 500;
 53         //判断D后面几个C,然后加上来
 54         count = Count(str,'C');
 55         S = S + count*100;
 56         count = 0;
 57     }
 58     //十位
 59     if('X' == str[index] && 'C' == str[index+1])
 60         S = S + 90,index+=2;
 61     else if('X' == str[index] && 'L' == str[index+1])
 62         S = S + 40,index+=2;
 63     else if('X' == str[index])
 64     {
 65         count = Count(str,'X');
 66         S = S + count*10;
 67     }
 68     else if('L' == str[index])
 69     {
 70         index++;
 71         S = S+50;
 72         count = Count(str,'X');
 73         S = S+count*10;
 74     }
 75     //个位
 76     if('I' == str[index] && 'X' == str[index+1])
 77         S = S+9,index+=2;
 78     else if('I' == str[index] && 'V' == str[index+1])
 79         S = S+4,index+=2;
 80     else if('I' == str[index])
 81     {
 82         count = Count(str,'I');
 83         S = S + count;
 84     }
 85     else if('V' == str[index])
 86     {
 87         index++;
 88         S = S+5;
 89         count = Count(str,'I');
 90         S = S+count;
 91     }
 92     return S;
 93 }
 94 int Count(char* s,char c)
 95 {
 96     int count = 0;
 97     if(c == s[index])
 98     {
 99         count++,index++;
100         if(c == s[index])
101         {
102             count++,index++;
103             if(c == s[index])
104             {
105                 count++,index++;
106             }    
107         }
108     }
109     return count;
110 }

 

posted @ 2018-03-28 23:14  来喽来喽  阅读(883)  评论(0编辑  收藏  举报