【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 }

 

转载于:https://www.cnblogs.com/bombe1013/p/3788485.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值