question 14th: convert roman numerals into integers
Memory limit 10000 K time limit 1000 ms
Given a Roman numeral S, (I<=S<=MMMCMXCIX) (i.e. 1 to 3999), converts Roman numerals to integers.
such as the Roman numeral i,ii,iii,iv,v respectively represent the numbers 1, 2, 3, 4, 5.
Format:
The first line enters a Roman digit, and then the corresponding integer is output.
Tips:
First of all, to understand the Roman numeral notation, there are 7 basic characters: I,v,x,l,c,d,m, respectively, representing 1,5,10,50,100,500,1000.
In the form of numbers, there are the following rules:
1, the same number of ligatures, the number represented is equal to the number of these numbers, such as: ⅲ= 3;
2, the small number in the large number of the right, the number represented equal to the number of these numbers, such as: ⅷ= 8;ⅻ= 12;
3, small number, (limited to Ⅰ, X and C) on the left of large numbers, the number represented is equal to the number of reduced number of large numbers, such as: ⅳ= 4;ⅸ= 9;
4, normal use, the number of ligatures should not repeat more than three times.
Sample input
Cxxiii
Sample output
123
for reference only:
#include <stdio.h>
#include <string.h>
void Main ()
{
char s[100];
int i,len,count=0;
scanf ("%s", &s);
len= strlen (s);
for (i=0;i<len;i++)
{
switch (s[i])
{case
' M ':
count+=1000;
break;
Case ' D ':
count+=500;
break;
Case ' C ':
if (s[i+1]== ' D ' | | s[i+1]== ' M ')
count-=100;
else
count+=100;
break;
Case ' L ':
count+=50;
break;
Case ' X ':
if (s[i+1]== ' L ' | | s[i+1]== ' C ')
count-=10;
else
count+=10;
break;
Case ' V ':
count+=5;
break;
Case ' I ':
if (s[i+1]== ' V ') | | s[i+1]== ' X ')
count--;
else
count++;
break;
Default:
printf ("error!\n");
}
printf ("%d\n", count);
}