hdu 1228 A + B

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1228

A + B

Description

读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.

Input

测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出. 

Output

对每个测试用例输出1行,即A+B的值.

Sample Input

one + two =
three four + five six =
zero seven + eight nine =
zero + zero =

Sample Output

3

90

96

字符串水题。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<string>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 using std::map;
11 using std::cin;
12 using std::cout;
13 using std::endl;
14 using std::vector;
15 using std::string;
16 #define sz(c) (int)(c).size()
17 #define all(c) (c).begin(), (c).end()
18 #define iter(c) decltype((c).begin())
19 #define cls(arr,val) memset(arr,val,sizeof(arr))
20 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
21 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
22 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
23 #define pb(e) push_back(e)
24 #define mp(a, b) make_pair(a, b)
25 const int Max_N = 100010;
26 typedef unsigned long long ull;
27 map<string, int> rec;
28 void init() {
29     rec["zero"] = 0;
30     rec["one"] = 1;
31     rec["two"] = 2;
32     rec["three"] = 3;
33     rec["four"] = 4;
34     rec["five"] = 5;
35     rec["six"] = 6;
36     rec["seven"] = 7;
37     rec["eight"] = 8;
38     rec["nine"] = 9;
39 }
40 int main() {
41 #ifdef LOCAL
42     freopen("in.txt", "r", stdin);
43     freopen("out.txt", "w+", stdout);
44 #endif
45     init();
46     int a, b, f;
47     char str[100], buf[100];
48     while (gets(buf)) {
49         a = b = f = 0;
50         char *p = buf;
51         while (true) {
52             if (*p == ' ') { p++; continue; }
53             sscanf(p, "%s", str);
54             if (!strcmp(str, "=")) break;
55             if (!f && strcmp(str, "+")) a = a * 10 + rec[str];
56             if (!strcmp(str, "+") || f) {
57                 if (!strcmp(str, "+")) { f = 1, p++; continue; }
58                 b = b * 10 + rec[str];
59             }
60             p = p + strlen(str);
61         }
62         if (!(a + b)) break;
63         printf("%d\n", a + b);
64     }
65     return 0;
66 }
View Code

 

posted @ 2015-06-23 16:35  GadyPu  阅读(133)  评论(0编辑  收藏  举报