## 题目1010：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  阅读(143)  评论(0编辑  收藏  举报