九度OJ-题目1010:A + B

来源:互联网 发布:java log4j用法 编辑:程序博客网 时间:2024/05/17 04:12

好吧,有是一道水题,毕竟先热一下身,自从来到苏州,整个天像蒸桑拿,慢慢适应吧。


题目如下:

题目描述:
读入两个小于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 =
样例输出:
39096

题目难度:水题中的水题(建议新手玩耍)

解题思路:

         其时也没什么好说的,就是个模拟。

c++代码

         

#include <iostream>#include <string.h>using namespace std;char strNum[10];char * nums[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};const char * el = "=";const char * add = "+";int getNum(const char * str){    for(int i = 0 ; i < 10;i++){        if(strcmp(str,nums[i])==0)return i;    }    return -1;}int main(){    int a=0,b=0,tag=0;    while(1){        cin>>strNum;        int t = getNum(strNum);        if(t!=-1&&tag==0){            a*=10;            a+=t;        }else if(t!=-1&&tag!=0){            b*=10;            b+=t;        }else if(strcmp(add,strNum)==0){            tag = 1;        }else if(strcmp(el,strNum)==0){            if(a==0&&b==0)break;            cout<<a+b<<endl;            a=0;b=0;tag=0;        }    }    return 0;}


       

0 0
原创粉丝点击