题目描述
Roman numerals are an ancient numbering system used extensively throughout Europe through the 13th century (where it was eventually replaced by our current positional system). Vestiges of this system still exist today on clock faces, building cornerstones, Super Bowls and Star Wars episodes. The system uses the following 7 symbols:
Symbols I, X, C and M can be repeated as needed (though never more than three times for I, X and C), so that 3 is represented as III, 27 as XXVII and 4865 as MMMMDCCCLXV. The symbols are always written from the highest value to the lowest, but for one exception: if a lower symbol precedes a higher one, it is subtracted from the higher. Thus 4 is written not as IIII but as IV, and 900 is written as CM.
The rules for this subtractive behavior are the following:
1. Only I, X and C can be subtracted.
2. These numbers can only appear once in their subtractive versions (e.g., you can’t write 8 as IIX).
3. Each can only come before symbols that are no larger than 10 times their value. Thus we can not write IC for 99 or XD for 490 (these would be XCIX and CDXC, respectively). Note that the first two words in this problem title are invalid Roman numerals, but the third is fine.
Your task for this problem is simple: read in a set of Roman numeral values and output their sum as a Roman numeral.
输入
Input will consist of multiple test cases. Each test case starts with a positive integer n indicating the number of values to add. After this will come n values (potentially several on a line), all valid Roman numerals with whitespace only coming between values. A value of n = 0 will indicate end of input. All sums will be less than 5000.
输出
For each test case, output the case number and the sum, both as Roman numerals, using the format shown below. Case numbers should start at I.
示例输入
2
XII MDL
4
I I I
I
0示例输出
Case I: MDLXII
Case II: IV
这道题目的内容很简单,大体的意思就是说先输入一个数字n(如果n==0,结束输入)然后接着输入n组罗马数据,把这n组数据累加起来得到的数字转化为罗马数字输出;
其中罗马数字数字转化为十进制数字的时候是先把罗马数字对应的字母的ASCII码值减去A的所得到的在数组le中对应的数字,值得注意一下的是前面的罗马数字有可能比后面的小,要相减,因为题目要求小于5000,所以说,在输出罗马数字的时候直接判断区间,输出;总之,除了有点麻烦之外,还是有点水的;
我的程序:
#include<stdio.h>
#include<string.h>
int le[26] = {0,0,100,500,0,0,0,0,1,0,0,50,1000,0,0,0,0,0,0,0,0,5,0,10,0,0};
int shu(char s[])
{
int n,c,i;
n=strlen(s);
c=0;
i=0;
if(n==1)
return le[s[i]-'A'];
for(i=0;i<n-1;i++)
{
if(le[s[i]-'A']<le[s[i+1]-'A'])
c=c-le[s[i]-'A'];
else
c=c+le[s[i]-'A'];
}
c=c+le[s[i]-'A'];
return c;
}
void print(int a)
{
int i,j;
if(a>=1000)
{
i=a/1000;
for(j=0;j<i;j++)
printf("M");
a-=1000*i;
}
if(a>=900)
{
printf("CM"); a-=900;
}
if(a>=500)
{
printf("D"); a-=500;
}
if(a>=400)
{
printf("CD"); a-=400;
}
if(a>=100)
{
i=a/100;
for(j=0;j<i;j++)
printf("C");
a-=100*i;
}
if(a>=90)
{
printf("XC"); a-=90;
}
if(a>=50)
{
printf("L"); a-=50;
}
if(a>=40)
{
printf("XL"); a-=40;
}
if(a>=10)
{
i=a/10;
for(j=0;j<i;j++)
printf("X");
a-=10*i;
}
if(a>=9)
{
printf("IX"); a-=9;
}
if(a>=5)
{
printf("V"); a-=5;
}
if(a>=4)
{
printf("IV"); a-=4;
}
for(j=0;j<a;j++)
printf("I");
}
int main()
{
int i,c,n,t,leap=0;
char str[100];
int a[4];
while(scanf("%d%*c",&n)&&n)
{
leap++;
c=0;
while(n--)
{
scanf("%s",str);
c=c+shu(str);
}
printf("Case ");
print(leap);
printf(": ");
print(c);
printf("\n");
}
return 0;
}
Roman numerals are an ancient numbering system used extensively throughout Europe through the 13th century (where it was eventually replaced by our current positional system). Vestiges of this system still exist today on clock faces, building cornerstones, Super Bowls and Star Wars episodes. The system uses the following 7 symbols:
Symbols I, X, C and M can be repeated as needed (though never more than three times for I, X and C), so that 3 is represented as III, 27 as XXVII and 4865 as MMMMDCCCLXV. The symbols are always written from the highest value to the lowest, but for one exception: if a lower symbol precedes a higher one, it is subtracted from the higher. Thus 4 is written not as IIII but as IV, and 900 is written as CM.
The rules for this subtractive behavior are the following:
1. Only I, X and C can be subtracted.
2. These numbers can only appear once in their subtractive versions (e.g., you can’t write 8 as IIX).
3. Each can only come before symbols that are no larger than 10 times their value. Thus we can not write IC for 99 or XD for 490 (these would be XCIX and CDXC, respectively). Note that the first two words in this problem title are invalid Roman numerals, but the third is fine.
Your task for this problem is simple: read in a set of Roman numeral values and output their sum as a Roman numeral.
输入
Input will consist of multiple test cases. Each test case starts with a positive integer n indicating the number of values to add. After this will come n values (potentially several on a line), all valid Roman numerals with whitespace only coming between values. A value of n = 0 will indicate end of input. All sums will be less than 5000.
输出
For each test case, output the case number and the sum, both as Roman numerals, using the format shown below. Case numbers should start at I.
示例输入
2
XII MDL
4
I I I
I
0示例输出
Case I: MDLXII
Case II: IV
这道题目的内容很简单,大体的意思就是说先输入一个数字n(如果n==0,结束输入)然后接着输入n组罗马数据,把这n组数据累加起来得到的数字转化为罗马数字输出;
其中罗马数字数字转化为十进制数字的时候是先把罗马数字对应的字母的ASCII码值减去A的所得到的在数组le中对应的数字,值得注意一下的是前面的罗马数字有可能比后面的小,要相减,因为题目要求小于5000,所以说,在输出罗马数字的时候直接判断区间,输出;总之,除了有点麻烦之外,还是有点水的;
我的程序:
#include<stdio.h>
#include<string.h>
int le[26] = {0,0,100,500,0,0,0,0,1,0,0,50,1000,0,0,0,0,0,0,0,0,5,0,10,0,0};
int shu(char s[])
{
int n,c,i;
n=strlen(s);
c=0;
i=0;
if(n==1)
return le[s[i]-'A'];
for(i=0;i<n-1;i++)
{
if(le[s[i]-'A']<le[s[i+1]-'A'])
c=c-le[s[i]-'A'];
else
c=c+le[s[i]-'A'];
}
c=c+le[s[i]-'A'];
return c;
}
void print(int a)
{
int i,j;
if(a>=1000)
{
i=a/1000;
for(j=0;j<i;j++)
printf("M");
a-=1000*i;
}
if(a>=900)
{
printf("CM"); a-=900;
}
if(a>=500)
{
printf("D"); a-=500;
}
if(a>=400)
{
printf("CD"); a-=400;
}
if(a>=100)
{
i=a/100;
for(j=0;j<i;j++)
printf("C");
a-=100*i;
}
if(a>=90)
{
printf("XC"); a-=90;
}
if(a>=50)
{
printf("L"); a-=50;
}
if(a>=40)
{
printf("XL"); a-=40;
}
if(a>=10)
{
i=a/10;
for(j=0;j<i;j++)
printf("X");
a-=10*i;
}
if(a>=9)
{
printf("IX"); a-=9;
}
if(a>=5)
{
printf("V"); a-=5;
}
if(a>=4)
{
printf("IV"); a-=4;
}
for(j=0;j<a;j++)
printf("I");
}
int main()
{
int i,c,n,t,leap=0;
char str[100];
int a[4];
while(scanf("%d%*c",&n)&&n)
{
leap++;
c=0;
while(n--)
{
scanf("%s",str);
c=c+shu(str);
}
printf("Case ");
print(leap);
printf(": ");
print(c);
printf("\n");
}
return 0;
}