题目1010:A + B

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2532

解决:1331

 
题目描述:
读入两个小于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年浙江大学计算机及软件工程研究生机试真题
 
 1 #include<iostream>
 2 #include<map>
 3 #include<queue>
 4 #include<math.h>
 5 using namespace std;
 6 string num_set[10] = {
 7     "zero","one","two","three","four",
 8     "five","six","seven","eight","nine"
 9 };
10 int main(){
11     map<string,int> string2dec;
12     for(int i = 0; i < 10; i++) string2dec[num_set[i]] = i;
13     string buff;
14     queue<int> numA,numB;
15     int A=0,B=0,powA = -1,powB=-1;
16     bool second_dec = false;
17     while(cin>>buff){
18         if(buff == "="){
19             while(!numA.empty()){
20                 A += numA.front() * (int)pow(10,powA--);
21                 numA.pop();
22             }
23             while(!numB.empty()){
24                 B += numB.front() * (int)pow(10,powB--);
25                 numB.pop();
26             }
27             if(A==0 && B==0) break;
28             cout<<A+B<<endl;
29             A=0,B=0,powA = -1,powB=-1;
30             second_dec = false;
31         }
32         else if(buff == "+") second_dec = true;
33         else{
34             int digit = (int)((*string2dec.find(buff)).second);
35             if(!second_dec){
36                 numA.push(digit);
37                 powA++;
38             }
39             else{
40                 numB.push(digit);
41                 powB++;
42             }
43         }
44     }
45     return 0;
46 }

 

posted on 2013-03-04 13:33  denallo  阅读(154)  评论(0编辑  收藏  举报

导航