熟悉的A+B
读入两个小于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<stdio.h> 2 #include<string.h> 3 int cmp(char s[]) 4 { 5 if(strcmp(s,"zero") ==0 ) return 0; 6 else if(strcmp(s,"one") ==0 ) return 1; 7 else if(strcmp(s,"two") == 0) return 2; 8 else if(strcmp(s,"three") == 0) return 3; 9 else if(strcmp(s,"four") == 0) return 4; 10 else if(strcmp(s,"five") == 0) return 5; 11 else if(strcmp(s,"six") == 0) return 6; 12 else if(strcmp(s,"seven") == 0) return 7; 13 else if(strcmp(s,"eight") == 0) return 8; 14 else if(strcmp(s,"nine") == 0) return 9; 15 return 0; 16 17 } 18 int main () 19 { 20 char s[10]; 21 int cnt = 0, i = 10, sum = 0, tmp = 0;因为题目说两位数之内,令i为10
22 while(~scanf("%s",s))用scanf读入。
23 { 24 if(strcmp(s,"+") == 0) 25 { 26 if(cnt == 1) 27 sum = tmp/10;如果加号前只有一个数,则加上temp/10;
28 else if(cnt == 2) 29 sum = tmp; 30 cnt = 0; 31 i = 10; 32 tmp = 0;重置
33 34 } 35 else if(strcmp(s,"=") == 0) 36 { 37 if(cnt == 1) 38 sum += tmp/10; 39 else if(cnt == 2) 40 sum += tmp; 41 if(sum == 0) 42 return 0;读入结束
43 else printf("%d\n",sum); 44 sum = 0; 45 i = 10; 46 cnt = 0; 47 tmp = 0; 48 } 49 else 50 { 51 cnt++;表示有几个数
52 tmp += cmp(s)*i; 53 i = i/10; 54 } 55 } 56 return 0; 57 }