輸入兩個英文數字詞組成的加法式(如 "one+two=three" 但實為 "one+two"),輸出數值和。數字詞:zero~nine。
map d 建立詞→值映射deal(s):按字符遍歷,識別數字詞邊界(由于是拼接無分隔符,需嘗試匹配最長詞)int deal(string s) {
static string words[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int ans=0;
for (int i=0;i=0;v--) {
string w=words[v];
if (s.substr(i,w.size())==w) {
ans=ans*10+v; i+=w.size(); found=true; break;
}
}
if (!found) i++; // 跳過非預期字符
}
return ans;
}
int main() {
map d;
string words[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
for(int i=0;i<10;i++) d[words[i]]=i;
string line;
while (getline(cin,line)) {
if (line.empty()) continue;
size_t p=line.find('+');
string s1=line.substr(0,p), s2=line.substr(p+1);
int n1=deal(s1), n2=deal(s2);
if (n1==0 && n2==0) break;
printf("%d\n", n1+n2);
}
return 0;
}
map 字符串映射是處理「詞→值」轉換的標準工具,注意最長匹配優先。