英文A+B

Description

小韩在ACM协会结识了一位外语学院的同学,她也喜欢编程,她也写了一个A+B的程序,两个小于100的正整数A和B,计算A+B,并且结果以二进制的形式输出。不过这个A+B有点特殊,A和B的每一位数字由对应的英文单词给出.

Input

测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.

Output

对每个测试用例输出1行,即A+B的值,结果以二进制输出。

Sample Input

one + two =
three four + five six =
zero seven + eight nine =
zero + zero =

Sample Output

11
1011010
1100000
模拟题
 
 1 #include <stdio.h>  
 2 #include <string.h>  
 3 #include <iostream>  
 4 #include <string>  
 5 #include <math.h>  
 6 #include <algorithm>  
 7 #include <vector>  
 8 #include <stack>  
 9 #include <queue>  
10 #include <set>  
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;  
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=1e5+10;
17 using namespace std;  
18 
19 int ans[maxn];
20 int len;
21 
22 void Show(int n)
23 {
24     len=0;
25     int t=n;
26     do{
27         ans[++len]=t%2;
28         t/=2;
29     }while(t);
30     for(int i=len;i>=1;i--)
31     {
32         cout<<ans[i];
33     }
34     cout<<"\n";
35 }
36 
37 int main()
38 {
39     string str;
40     while(getline(cin,str))
41     {
42         map<string,int> mp;
43         mp["zero"]=0;mp["one"]=1;mp["two"]=2;mp["three"]=3;mp["four"]=4;
44         mp["five"]=5;mp["six"]=6;mp["seven"]=7;mp["eight"]=8;mp["nine"]=9;
45         istringstream is(str);
46         string tem;
47         int a=0,b=0;
48         int flag=0;
49         while(is>>tem)
50         {
51             if(flag==0&&tem!="+"&&tem!="=")
52                 a=a*10+mp[tem];
53             else if(tem=="+")
54                 flag=1;
55             else if(flag==1&&tem!="+"&&tem!="=")
56                 b=b*10+mp[tem];
57         }
58         if(a==0&&b==0)
59             break;
60         int sum=a+b;
61         Show(sum);
62     }
63 }
 
 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11681500.html