hdu 1228 A + B

本文介绍了一个使用C++编写的程序,该程序能够将英文形式的两位数相加并输出其数值结果。通过预先定义的映射表,程序可以识别如'two'和'three'这样的英文单词,并将其转换为对应的数字进行加法运算。

http://acm.hdu.edu.cn/showproblem.php?pid=1228

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;

int main()
{
    string str1,str4,strp,str2,str5,stre;
	map<string,int>m1;
	int num1,num2;

	m1["one"]=1,m1["two"]=2,m1["three"]=3,m1["four"]=4,m1["five"]=5;
	m1["six"]=6,m1["seven"]=7,m1["eight"]=8,m1["nine"]=9,m1["zero"]=0;

    while(cin>>str1>>strp)
    {
		if(strp!="+")
		{
			cin>>str4;
			num1=m1[str1]*10+m1[strp];  //map的使用
		}
		else
			num1=m1[str1];

		cin>>str2>>stre;
		if(stre!="=")
		{
			cin>>str5;
			num2=m1[str2]*10+m1[stre];
		}
		else
			num2=m1[str2];
		if(num1==0&&num2==0)
			break;
		cout<<num1+num2<<endl;
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值