CIVIC DILL MIX

来源:互联网 发布:苹果电脑屏幕录像软件 编辑:程序博客网 时间:2024/05/06 07:21

CIVIC DILL MIX

Time Limit: 1000MS Memory limit: 65536K

题目描述

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.

示例输入

2XII MDL4I I II0

示例输出

Case I: MDLXIICase II: IV
这个题大体思路先换成数字再转换
#include<stdio.h>#include<string.h>void f1(int s);int f(char a){if(a=='I') return 1;if(a=='V')  return 5;if(a=='X')  return 10;if(a=='L') return 50;if(a=='C')  return 100;if(a=='D')  return 500;    if(a=='M') return 1000;}void f1(int s){int s1,s2,s3,s4,s21,s31,s41,j; s1=s/1000;for(j=1;j<=s1;j++) printf("M");s2=(s-s1*1000)/100;if(s2==4) printf("CD");else if(s2==9) printf("CM");else if(s2==5) printf("D");else  if(s2>5&&s2<9) {printf("D");s21=s2-5;s21=s2-5;                      for(j=1;j<=s21;j++) printf("C");}else    for(j=1;j<=s2;j++) printf("C");s3=(s-s1*1000-s2*100)/10;if(s3==4) printf("XL");else if(s3==9) printf("XC");else if(s3==5) printf("L");else  if(s3>5&&s3<9) {printf("L");s31=s3-5;s31=s3-5;                      for(j=1;j<=s31;j++) printf("X");}else    for(j=1;j<=s3;j++) printf("X");s4=s%10;if(s4==4) printf("IV");else if(s4==9) printf("IX");else if(s4==5) printf("V");else  if(s4>5&&s4<9) {printf("V");s41=s4-5;s41=s4-5;                      for(j=1;j<=s41;j++) printf("I");}else    for(j=1;j<=s4;j++) printf("I");}int main(){int n,i,j,k1,s,num,s1,s2,s3,s4,s5,s21,s31,s41,count=0;char st1[1000];while(scanf("%d",&n)!=EOF&&n!=0){count++;s=0;for(i=0;i<=n-1;i++){    scanf("%s",st1);     k1=strlen(st1);     for(j=0;j<=k1-1;j++){                 if(st1[j]=='I'&&(st1[j+1]=='V'||st1[j+1]=='X'))   {num=f(st1[j+1])-f(st1[j]);j++;}   else if(st1[j]=='X'&&(st1[j+1]=='L'||st1[j+1]=='C'))               {num=f(st1[j+1])-f(st1[j]);j++;}   else if(st1[j]=='C'&&(st1[j+1]=='D'||st1[j+1]=='M'))               {num=f(st1[j+1])-f(st1[j]);j++;}   else    num=f(st1[j]); s+=num;}  }printf("Case ");f1(count);printf(": ");        f1(s);                printf("\n");}return 0;}