Description
读入两个小于10000的正整数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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include<stdio.h> #include<string.h> #include<math.h> int prem( char a[]) { int re; if ( strcmp ( "zero" ,a)==0) re=0; else if ( strcmp ( "one" ,a)==0) re=1; else if ( strcmp ( "two" ,a)==0) re=2; else if ( strcmp ( "three" ,a)==0) re=3; else if ( strcmp ( "four" ,a)==0) re=4; else if ( strcmp ( "five" ,a)==0) re=5; else if ( strcmp ( "six" ,a)==0) re=6; else if ( strcmp ( "seven" ,a)==0) re=7; else if ( strcmp ( "eight" ,a)==0) re=8; else if ( strcmp ( "nine" ,a)==0) re=9; return re; } int main() { int i,j,m,n,point,len,n1[10],n2[10],sum1,sum2; char str[10000],str1[20]; while ( gets (str)) { if ( strcmp ( "zero + zero =" ,str)==0) return 0; m=0;n=0;sum1=0,sum2=0; len= strlen (str); for (point=0;point<len;point++) if (str[point]== '+' ) break ; for (i=0,j=0;i<point;i++) { str1[j]=str[i]; if (str1[j]== ' ' ) { str1[j]= '\0' ; n1[m++]=prem(str1); j=0; continue ; } j++; } for (i=point+2,j=0;i<len;i++) { str1[j]=str[i]; if (str1[j]== ' ' ) { str1[j]= '\0' ; n2[n++]=prem(str1); j=0; continue ; } j++; } for (i=0;i<m;i++) sum1+=( pow (10,m-1-i))*n1[i]; for (i=0;i<n;i++) sum2+=( pow (10,n-1-i))*n2[i]; printf ( "%d\n" ,sum1+sum2); } } /************************************************************** Problem: 1182 User: TianChenguang Language: C Result: Accepted Time:0 ms Memory:1280 kb ****************************************************************/ |