HDOJ.1228 A + B (map)
A + B
题意分析
讲字符串和数字用map对应即可
代码总览
/*
Title:HDOJ.1228
Author:pengwill
Date:2016-11-21
*/
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef map<string,int> mmp;
mmp p;
mmp::iterator iter1,iter2,iter3;
string s1,s2,s3,s4,s5,s6;
int main()
{
cin.sync_with_stdio(false);
cin.tie(0);
p["zero"] = 0;
p["one"] = 1;
p["two"] = 2;
p["three"] = 3;
p["four"] = 4;
p["five"] = 5;
p["six"] = 6;
p["seven"] = 7;
p["eight"] = 8;
p["nine"] = 9;
while(cin>>s1>>s2>>s3){
if(s2.compare("+") == 0){
if(s1.compare("zero")==0 && s3.compare("zero")==0){
break;
}else{
cin>>s4;
if(s4.compare("=") == 0){
iter1 = p.find(s1);iter2 = p.find(s3);
cout<<iter1->second + iter2->second<<endl;
}else{
int a;
cin>>s5;
iter3 = p.find(s1);
iter1 = p.find(s3);iter2 = p.find(s4);
a = iter1->second * 10 + iter2->second;
cout<<a+ iter3->second <<endl;
}
}
}else{
int a,b;
iter1 = p.find(s1);iter2 = p.find(s2);
a = iter1->second * 10 + iter2->second;
cin>>s1>>s2;
if(s2.compare("=") == 0){
// cout<<s2<<endl;
iter3 = p.find(s1);
b = iter3->second;
// cout<<s1<<endl;
// cout<<iter3->second<<endl;
cout<<a + b<<endl;;
}else{
cin>>s3;
iter1 = p.find(s1);iter2 = p.find(s2);
b = iter1->second * 10 + iter2->second;
cout<<a+b<<endl;
}
}
}
return 0;
}