原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1228
A + B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21350 Accepted Submission(s): 12777
需要注意的是:A和B的每一位數字由對應的英文單詞給出.
題記:
本題使用了STL中的map,把string類型的數字轉化爲對應int型的數字,然後進行計算。
map的使用非常簡單,話不多說,直接看代碼吧。
C++代碼:
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
int main(){
map<string, int> num;
num["zero"] = 0;
num["one"] = 1;
num["two"] = 2;
num["three"] = 3;
num["four"] = 4;
num["five"] = 5;
num["six"] = 6;
num["seven"] = 7;
num["eight"] = 8;
num["nine"] = 9;
int a, b;
string s;
for( ; ; ) {
a = 0; //a存放+號前的數
while(cin >> s) {
if(s=="+"){
break;
}
a = a * 10 + num[s];
}
b = 0; //b存放+號後的數
while(cin >> s) {
if(s == "="){
break;
}
b = b * 10 + num[s];
}
if(a == 0 && b == 0){
break;
}else{
cout << a + b << endl;
}
}
return 0;
}