九度OJ 1010: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
思路:
基本的字符串处理题,没什么难度。
C语言读取一行可以用gets(s)。
代码:
#include <stdio.h> #include <string.h> char praseStr(char s[20]) { char c; switch(s[0]) { case 'z': c = '0'; break; case 'o': c = '1'; break; case 't': if (strcmp(s, "two") == 0) c = '2'; else c = '3'; break; case 'f': if (strcmp(s, "four") == 0) c = '4'; else c = '5'; break; case 's': if (strcmp(s, "six") == 0) c = '6'; else c = '7'; break; case 'e': c = '8'; break; case 'n': c = '9'; break; default: c = s[0]; break; } return c; } int main(void) { char s[6][20]; char c[6]; int count; int a, b; while (1) { count = 0; while (scanf("%s", s[count])) { c[count] = praseStr(s[count]); count ++; if (c[count-1] == '=') break; } a = 0; b = 0; for (int i=0; i<count; i++) { if (c[i] == '+' || c[i] == '=') continue; if (i < 2) a = a*10 + c[i]-48; else b = b*10 + c[i]-48; //printf("%d\n", a); //printf("%d\n", b); } if (a == 0 && b == 0) break; printf("%d\n", a+b); } return 0; } /************************************************************** Problem: 1010 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/
编程算法爱好者。