九度OnlineJudge之1010 A + B

来源:互联网 发布:h3c mac acl 编辑:程序博客网 时间:2024/05/18 01:03
题目描述:                       
读入两个小于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
#include <iostream>#include <string>#include <vector>#include <cmath>using namespace std;inline int  getInt(string  str){if (str=="zero")  return 0;if (str=="one")  return 1;if (str=="two")  return 2;if (str=="three")  return 3;if (str=="four")  return 4;if (str=="five")  return 5;if (str=="six")  return 6;if (str=="seven")  return 7;if (str=="eight")  return 8;if (str=="nine")  return 9;return -1;}int main(){vector<string>   vec1,vec2;vector<string>::iterator it;     string   tmp1,tmp2;int A,B;while(true){A=0;        B=0;vec1.clear();vec2.clear();while(cin>>tmp1,tmp1!="+") vec1.push_back(tmp1);while(cin>>tmp2,tmp2!="=") vec2.push_back(tmp2);int len1 = vec1.size();int len2 = vec2.size();        bool  flag = (*(vec1.begin())=="zero" && *(vec2.begin())=="zero" &&len1==1 &&len2==1);    if (flag)    break;for (int i=0;i<len1;i++){A = A + getInt(vec1[i]) * (int)pow(10.0,(double)len1-1-i);}for (int i=0;i<len2;i++){B = B + getInt(vec2[i]) * (int)pow(10.0,(double)len2-1-i);}cout<<A+B<<endl;}//system("pause");return 0;}

原创粉丝点击