http://acm.sdut.edu.cn/web/problem.php?action=showproblem&problemid=2216
算是模拟题吧,就是给你几个罗马数字表示的数,求他们的和然后再用罗马数字表示输出
罗马数字表示的时候有些规则注意就是了。其实也是很简单的
思路:先把 1~10,10~100,100~1000 这27个整数用罗马表示写到三个字符数组里,然后把给定的罗马数转换为我们的十进制数,相加后,再把这些数转换成罗马数输出就行了
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<string>
5 using namespace std;
6 char str1[][10]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
7 char str10[][10]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
8 char str100[][10]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
9 #define N 5010
10 int chang(char s[])
11 {
12 int len=strlen(s)-1;
13 int temp=0,ans=0;
14 int sum=0;
15 while(len>=0)
16 {
17 switch(s[len])
18 {
19 case 'I':temp=1;break;
20 case 'V':temp=5;break;
21 case 'X':temp=10;break;
22 case 'L':temp=50;break;
23 case 'C':temp=100;break;
24 case 'D':temp=500;break;
25 case 'M':temp=1000;break;
26 }
27 if(temp<ans)sum-=temp;
28 else sum+=temp;
29 ans=temp;
30 len--;
31 }
32 return sum;
33 }
34 void cat(int sum,int k)
35 {
36 char s[N];
37 int i;
38 memset(s,0,sizeof(s));
39 int tem;
40 tem=sum/1000;
41 for( i=0;i<tem;i++)
42 {
43 strcat(s,"M");
44 }
45 s[i]='\0';
46 sum%=1000;
47 tem=sum/100;
48 strcat(s,str100[tem]);
49 sum%=100;
50 tem=sum/10;
51 strcat(s,str10[tem]);
52 sum%=10;
53 strcat(s,str1[sum]);
54 if(k==1)printf("Case %s:",s);
55 else cout<<" "<<s<<endl;
56
57 }
58 int main()
59 {
60 int t,cs=0;
61 char sbr[N];
62 int i;
63 while(cin>>t,t)
64 {
65 ++cs;
66 int ans=0;
67 for(i=0;i<t;i++)
68 {
69 cin>>sbr;
70 ans+=chang(sbr);
71 }
72 cat(cs,1);
73 cat(ans,0);
74 }
75 return 0;
76 }