A+B
题目描述
读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出.
输入描述:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
输出描述:
对每个测试用例输出1行,即A+B的值.
示例1
输入
one + two = three four + five six = zero seven + eight nine = zero + zero =
输出
3 90 96
解题思路:用map来存储数值方便操作,讨论四种情况即可。
/* * @Qusetion: https://dwz.lc/iiYuaA2eO * @Author: 一届书生 * @Date: 2020-03-19 10:13:14 * @LastEditTime: 2020-03-21 23:54:26 */ #include <iostream> #include <string> #include <map> using namespace std; char s[50]; int main() { map<string, int> mp{{"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}, {"zero", 0}}; while (gets(s)) { string t[10]; for (int i = 0, j = 0; i < 50; i++) { if (s[i] == ' ') j++; else t[j] += s[i]; } if (t[0] == t[2] && t[2] == "zero") return 0; // *+* if (t[1] == "+" && t[3][0] == '=') cout << mp[t[0]] + mp[t[2]] << endl; // *+** if (t[1] == "+" && t[4][0] == '=') cout << mp[t[0]] + mp[t[2]] * 10 + mp[t[3]] << endl; // **+* if (t[2] == "+" && t[3][0] == '=') cout << mp[t[0]] * 10 + mp[t[1]] + mp[t[3]] << endl; // **+** if (t[2] == "+" && t[5][0] == '=') cout << mp[t[0]] * 10 + mp[t[1]] + mp[t[3]] * 10 + mp[t[4]] << endl; } return 0; }