HDU 1228(A + B)

基础题,使用 map 数据结构存储单词和对应的数字,依次读取 '+' 左边和右边的数,相加即可。

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

int main()
{
	map<string, int> m; //单词-数字
    m["zero"] = 0;
    m["one"] = 1;
    m["two"] = 2;
    m["three"] = 3;
    m["four"] = 4;
    m["five"] = 5;
    m["six"] = 6;
    m["seven"] = 7;
    m["eight"] = 8;
    m["nine"] = 9;
    m["ten"] = 10;

	string s; //输入
    while (true)
    {
        int a = 0, b = 0; //加数
        while (cin >> s) //读入+号左边的数
        {
            if (s == "+")
                break;
            a = a * 10 + m[s];
        }
        while (cin >> s) //读入+号右边的数
        {
            if (s == "=")
                break;
            b = b * 10 + m[s];
        }
        if (a == 0 && b == 0)
            break;
        cout << a + b << endl;
    }
	return 0;
}

继续加油。

发布了152 篇原创文章 · 获赞 1 · 访问量 7620

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/104689980