ACM水题-英文A+B(AC解题报告)

Posted on 2012-08-13 15:18 Seed-L 阅读(147) 评论(0)  编辑 收藏 引用

英文A+B
Time Limit:1000MS  Memory Limit:32768K

Description:

输入两个小于100的正整数A和B,计算A+B。注意:A和B的每一个数字由对应的英文单词组成。

Input:

测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔。当A和B同时为zero时输入结束,相应的结果不要输出.

Output:

对每个测试用例输出1行,即A+B的值.

Sample Input:

one + two =
three four + five six =
seven + eight nine =
one two + two zero =
zero + zero =

Sample Output:

3
90
96
32

Source:

hxm
Status  Submit


题目链接:http://acm.zjut.edu.cn/ShowProblem.aspx?ShowID=13
 1#include<stdio.h>
 2#include<stdlib.h>
 3#include<string.h>
 4#include<stdbool.h>
 5
 6char *pszStrNums[] = {"zero","one","two","three","four",
 7                    "five","six","seven","eight","nine"
 8                }
 ;
 9
10int main(void)
11{
12    char szStr[100] ;
13    char szStrA[5] ;
14    char szStrB[5] ; 
15    char *pszStr = NULL ;
16    int nA = 0 ;
17    int nB = 0 ;
18    int nAIndex = 0 ;
19    int nBIndex = 0 ;
20    int i = 0 ;
21    bool fNext = 0 ;
22
23    while(gets(szStr) != NULL)
24    {
25        pszStr = strtok(szStr," ") ;
26        nA = 0 ;
27        nB = 0 ;
28        fNext = 0 ;
29        nAIndex = 0 ;
30        nBIndex = 0 ;
31
32        while(strcmp(pszStr,"="!= 0 )
33        {
34            if(0 == strcmp(pszStr,"+"))
35            {
36                fNext = 1 ; 
37                szStrA[nAIndex] = '\0' ;
38            }

39
40            for(i = 0 ; i < 10 ; ++i)
41            {
42                if(0 == strcmp(pszStr,pszStrNums[i]))
43                {
44                    if(0 == fNext)
45                    {
46                        szStrA[nAIndex++= i + '0' ;
47                    }

48                    else
49                    {
50                        szStrB[nBIndex++= i + '0' ;
51                    }

52                    break ;
53                }

54            }

55            pszStr = strtok(NULL," ") ;
56        }

57
58        szStrB[nBIndex] = '\0' ;
59
60        nA = atoi(szStrA) ;
61        nB = atoi(szStrB) ;
62
63        if(0 == nA && 0 == nB)
64        {
65            break ;
66        }

67        else
68        {
69            printf("%d\n",nA+nB) ;
70        }

71    }

72    return 0 ;
73}
87



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


posts - 5, comments - 1, trackbacks - 0, articles - 3

Copyright © Seed-L