码迷,mamicode.com
首页 > 其他好文 > 详细

A + B

时间:2015-02-23 10:51:07      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

   

题目描述:
读入两个小于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
来源:
2005年浙江大学计算机及软件工程研究生机试真题

 

    #include<stdio.h>
    #include<string.h>

    int f(char s[])
    {
            if(s[0] == ‘z‘)                        return 0;
            else if(s[0] == ‘o‘)                   return 1;
            else if(s[0] == ‘t‘ && s[1] == ‘w‘)    return 2;
            else if(s[0] == ‘t‘ && s[1] == ‘h‘)    return 3;
            else if(s[0] == ‘f‘ && s[1] == ‘o‘)    return 4;
            else if(s[0] == ‘f‘ && s[1] == ‘i‘)    return 5;
            else if(s[0] == ‘s‘ && s[1] == ‘i‘)    return 6;
            else if(s[0] == ‘s‘ && s[1] == ‘e‘)    return 7;
            else if(s[0] == ‘e‘)                   return 8;
            else if(s[0] == ‘n‘)                  return 9;
            else return -1;
    }

    int main(void)
    {
            char s[5];

            while(scanf("%s",s) != EOF)
            {
                    int a,b;
                    a = f(s);
                    while(scanf("%s",s) != EOF)
                    {
                            if(s[0] != ‘+‘ && s[0] != ‘=‘)
                            {
                                    a *= 10;
                                    a += f(s);
                            }
                            else if(s[0] == ‘+‘)
                            {
                                    b = a;
                                    a = 0;
                            }
                            else if(s[0] == ‘=‘)
                                    break;
                    }
                    if(a || b)
                            printf("%d\n",a+b);
                    else
                            return 0;
            }
            return 0;
    }

A + B

标签:

原文地址:http://www.cnblogs.com/wft1990/p/4297897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!