题解 P2108 【学英语】

2021.1.2 Update:更好的排版与推广博客

博客:https://www.luogu.com.cn/blog/332914/

这个题目通常有两种解法,打表和map,打表要写很多 if,而 map 其实也是这样。

先要注意一点:

碰到一千和一百万是乘而不是加,例如 one hundred 不是 1+100\large 1 + 100 而是1100\large 1 * 100

先看打表法子,很麻烦,138ms:

#include <iostream>
using namespace std;

string s;

int main()
{
    int ans = 0, x = 0;
	while(cin >> s)
    {
		if(s == "negative")
        {
            cout << "-";
        }
		if(s == "one")
        {
            x++;
        }	
		if(s == "two")
        {
            x += 2;
        }	
		if(s == "three")
        {
            x += 3;
        }	
		if(s == "four")
        {
            x += 4;
        }
		if(s == "five")
        {
            x += 5;
        }
		if(s == "six")
        {
            x += 6;
        }	
		if(s == "seven")
        {
            x += 7;
        }	
		if(s == "eight")
        {
            x += 8;
        }	
		if(s == "nine")
        {
            x += 9;
        }	
		if(s == "ten")
        {
            x += 10;
        }	
		if(s == "eleven")
        {
            x += 11;
        }	
		if(s == "twelve")
        {
            x += 12;
        }	
		if(s == "thirteen")
        {
            x += 13;
        }	
		if(s == "fourteen")
        {
            x += 14;
        }	
		if(s == "fifteen")
        {
            x += 15;
        }	
		if(s == "sixteen")
        {
            x += 16;
        }	
		if(s == "seventeen")
        {
            x += 17;
        }
		if(s == "eighteen")
        {
            x += 18;
        }
		if(s == "nineteen")
        {
            x += 19;
        }	
		if(s == "twenty")
        {
            x += 20;
        }	
		if(s == "thirty")
        {
            x += 30;
        }	
		if(s == "forty")
        {
            x += 40;
       	}
		if(s == "fifty")
        {
            x += 50;
        }	
		if(s == "sixty")
        {
            x += 60;
        }	
		if(s == "seventy")
        {
            x += 70;
        }	
		if(s == "eighty")
        {
            x += 80;
        }	
		if(s == "ninety")
        {
            x += 90;
        }
		if(s == "hundred")
        {
            x *= 100;
        }
		if(s == "thousand")
        {
            ans += x * 1000;
            x = 0;
        }	
		if(s == "million")
        {
            ans += x * 1000000;
            x = 0;
        }
	}
	ans += x;
	cout << ans << endl;
}

还有一种就是map了,map一会会讲讲,137ms,快了1ms,但写的少很多:

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

map <string, int> m;

string s;

int main()
{
    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;
    m["eleven"] = 11;
    m["twelve"] = 12;
    m["thirteen"] = 13;
    m["fourteen"] = 14;
    m["fifteen"] = 15;
    m["sixteen"] = 16;
    m["seventeen"] = 17;
    m["eighteen"] = 18;
    m["nineteen"] = 19;
    m["twenty"] = 20;
    m["thirty"] = 30;
    m["forty"] = 40;
    m["fifty"] = 50;
    m["sixty"] = 60;
    m["seventy"] = 70;
    m["eighty"] = 80;
    m["ninety"] = 90;
    int ans = 0, c = 0;
    while(cin >> s != 0)
    {
        if(s == "negative")
        {
            cout << "-";
        }
        ans += m[s];
        if(s == "hundred")
        {
            ans *= 100;
        }
        if(s == "thousand")
        {
            c += ans * 1000;
            ans = 0;
        }
    	if(s == "million")
        {
            c += ans * 1000000;
            ans = 0;
        }
    }
    c += ans;
    cout << c << endl;
    return 0;
}

好,现在讲下map

大家都知道数组,比如:

int arr[10005], x, y;
cin >> x >> y;
arr[x] = y;

数组下标只能为int,但如果像这道题目,数组肯定不行

用map可以做到下标和值为任何数据类型

map是STL容器中的映射类型,定义方式如下:

map <下标数据类型,值数据类型> 映射名称;

如:

map <int, string> ma;
  
ma[666] = "AC";
  
cout << ma[666] << endl;

运行,输出:AC

map常用函数有:

clear()

begin()

end()

erase()

和vector等STL容器一样,也有迭代器

声明如下(两种)

1、

map <下标数据类型,值数据类型>::iterator 迭代器名称;

2、

auto 迭代器名称 = 值;

注意,迭代器和map映射map <下标数据类型,值数据类型>要一样

迭代器的便利方式是这样的

例如一下代码:

map <string, int> ma;
  
ma["a"] = 1;
ma["c"] = 2;
ma["b"] = 3;
  
for(map <string, int>::iterator it = ma.begin(); it != ma.end(); it++)
{
    cout << it -> first << " " << it -> second << " ";
}

运行结果:a 1 b 3 c 2

可以看出,map在建立映射时,会将下标从小到大排序,first输出下标,second输出值,输入也是一样,但输入不能输入first的下标。

posted @   HappyBobb  阅读(8)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示