E - A + B解题报告

来源:互联网 发布:公司域名 编辑:程序博客网 时间:2024/06/05 13:33
E - A + B
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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

39096 当时就认为需要对字符串英文与整数进行相互转换,于是用了一个字符串的数组。下标就是它代表的整数。
//A + B#include <stdio.h>#include <string.h>int GetIntFromWord(char n[]);char num[10][10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};void main(){char n[10];int a1,a2,b1,b2;while (1){scanf("%s",n);a1 = GetIntFromWord(n);getchar();scanf("%s",n);if (strcmp(n,"+") != 0){a2 = GetIntFromWord(n);getchar();getchar();}elsea2 = -1;getchar();scanf("%s",n);b1 = GetIntFromWord(n);getchar();scanf("%s",n);if (strcmp(n,"=") != 0){b2 = GetIntFromWord(n);getchar();getchar();}elseb2 = -1;if (a2 != -1)a1 = a1 * 10 + a2;if (b2 != -1)b1 = b1 * 10 + b2;if (a1 == 0 && b1 == 0)break;printf("%d\n",a1 + b1);}}int GetIntFromWord(char n[]){int i;for (i = 0;i < 10;i ++){if (strcmp(n,num[i]) == 0)return i;}return -1;}

0 0
原创粉丝点击