当前位置: 代码迷 >> 综合 >> HDU--1228--A+B问题
  详细解决方案

HDU--1228--A+B问题

热度:53   发布时间:2023-12-12 06:19:30.0

读入两个小于100的正整数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
3
90
96

思路:1.构造一个dex数组用于直接查询对应的数字;

2.对于输入格式需要注意:xx xx + xx xx = 以及xx xx + xx = 都容易出错,需要提前判断"+"和"="出现的位置;

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
const int maxa=1e2+10;
char z[maxa],zz[maxa],y[maxa],yy[maxa];
char ja[maxa],den[maxa];
char dex[12][maxa]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};//len=12
int znum,ynum;
int function(char *a){for(int i=0;i<12;i++)if(!strcmp(a,dex[i]))	return i;
}
int main(){while(true){cin>>z;cin>>ja;if(!strcmp(ja,"+"))	znum=function(z);else{ 	cin>>zz;	znum=10*function(z)+function(ja);} cin>>y;cin>>den;if(!strcmp(den,"="))	 ynum=function(y);else{cin>>yy;ynum=10*function(y)+function(den);} if(!strcmp(z,dex[0])&&!strcmp(y,dex[0]))	break;cout<<znum+ynum<<endl;}
}