ACM&a+b

A + B

题目描述:
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
输入:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
输出:
对每个测试用例输出1行,即A+B的值.
样例输入:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
样例输出:
3
90
96
http://ac.jobdu.com/problem.php?pid=1010
main.cpp
 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 /************************
 7 
 8 one
 9 
10 two three
11 
12 four five
13 
14 six seven
15 
16 eight
17 
18 nine
19 
20 zero
21 
22 ************************/
23 
24 int trans(char a[], int i);
25 int num_len(int i);
26 
27 int main()
28 {
29     char a[50];
30     int len,x,y,i,j;
31     while(cin.getline(a,50)) {
32         len = strlen(a);
33         x=0;
34         y=0;
35         for(i=0; i<len&&a[i]!='+'; ) {
36             if(a[i]!=' ') {
37                 j=trans(a,i);
38                 x=x*10+j;
39                 i+=num_len(j);
40             }
41             else i++;
42         }
43         for(i++;i<len&&a[i]!='='; ) {
44             if(a[i]!=' ') {
45                 j=trans(a,i);
46                 y=y*10+j;
47                 i+=num_len(j);
48             }
49             else i++;
50         }
51         if(x==0&&y==0)
52             break;
53         cout<<x+y<<endl;
54     }
55     return 0;
56 }
57 
58 int trans(char a[], int i) {
59     switch(a[i]) {
60         case 'o': return 1; break;
61         case 't': return a[i+1]=='w' ? 2 : 3; break;
62         case 'f': return a[i+1]=='o' ? 4 : 5; break;
63         case 's': return a[i+1]=='i' ? 6 : 7; break;
64         case 'e': return 8; break;
65         case 'n': return 9; break;
66         case 'z': return 0;
67         break;
68     }
69     return -1;
70 }
71 
72 int num_len(int i) {
73     switch(i) {
74         case 1: return 4; break;
75         case 2: return 4; break;
76         case 3: return 5; break;
77         case 4: return 5; break;
78         case 5: return 5; break;
79         case 6: return 4; break;
80         case 7: return 6; break;
81         case 8: return 6; break;
82         case 9: return 5; break;
83         case 0: return 5; break;
84     }
85     return 0;
86 }

 

posted on 2013-03-12 22:12  Zachary_wiz  阅读(178)  评论(0编辑  收藏  举报

导航