【HDOJ】1924 CIVIC DILL MIX

简单字符串。

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MAXN 55
 5 char srca[MAXN], cas[MAXN], sums[MAXN];
 6 
 7 inline int getVal(char ch) {
 8     switch (ch) {
 9     case 'I': return 1;
10     case 'V': return 5;
11     case 'X': return 10;
12     case 'L': return 50;
13     case 'C': return 100;
14     case 'D': return 500;
15     case 'M': return 1000;
16     default:  return 0;
17     }
18 }
19 
20 int R2I(char str[]) {
21     int i, len = strlen(str);
22     int val = 0;
23     for (i=0; i<len; ++i) {
24         if (str[i]=='C' && (str[i+1]=='D'||str[i+1]=='M'))
25             val -= 100;
26         else if (str[i]=='X' && (str[i+1]=='L'||str[i+1]=='C'))
27             val -= 10;
28         else if (str[i]=='I' && (str[i+1]=='X'||str[i+1]=='V'))
29             val -= 1;
30         else
31             val += getVal(str[i]);
32     }
33     return val;
34 }
35 
36 void f1(int *p, int *q, char str[], int a, int b, char ca, char cb, char cc) {
37     int x = *p, t=a/9;
38     int i = *q;
39     if (x >= a) {
40         x -= a;
41         str[i++] = ca;
42         str[i++] = cc;
43     }
44     if (x >= b) {
45         x -= b;
46         str[i++] = cb;
47     }
48     if (x >= (a-b)) {
49         x -= (a-b);
50         str[i++] = ca;
51         str[i++] = cb;
52     }
53     while (x >= t) {
54         str[i++] = ca;
55         x -= t;
56     }
57     *p = x;
58     *q = i;
59 }
60 
61 void I2R(int x, char str[]) {
62     int i = 0;
63     while (x >= 1000) {
64         str[i++] = 'M';
65         x -= 1000;
66     }
67     f1(&x, &i, str, 900, 500, 'C', 'D', 'M');
68     f1(&x, &i, str, 90, 50, 'X', 'L', 'C');
69     f1(&x, &i, str, 9, 5, 'I', 'V', 'X');
70     str[i] = '\0';
71 }
72 
73 int main() {
74     int c;
75     int n, case_n = 1;
76 
77     while(scanf("%d", &n)!=EOF && n) {
78         c = 0;
79         while (n--) {
80             scanf("%s", srca);
81             c += R2I(srca);
82         }
83         I2R(case_n, cas);
84         I2R(c, sums);
85         printf("Case %s: %s\n", cas, sums);
86         ++case_n;
87     }
88 
89     return 0;
90 }

 

posted on 2014-06-14 17:49  Bombe  阅读(221)  评论(0编辑  收藏  举报

导航