三、搜索和二分 [Cloned] C - map

原题:

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

题意:

到达一个新地方,不会当地的语言,但是有一本字典,输入当地语言,翻译成英语。

题解:

map的简单应用,创建一个<string,string>型的map,因为如果输入找不到对应的翻译就要输出一个eh,所以在创建一个<string,string>型的map来记录这个单词有没有出现过,别的就是简单的map输入问题了。

代码:AC

#include<iostream>
#include<string>
#include<map>
using namespace std;
 
int main()
{
	char english[11],foreign[11];
	map<string,bool>appear;
	map<string,string>translate;
	while(true)
	{
		char t;
		if((t=getchar())=='\n')
			break;
		else
		{
			english[0]=t;
			int i=1;
			while(true)
			{
				t=getchar();
				if(t==' ')
				{
					english[i]='\0';
					break;
				}
				else
					english[i++]=t;
			}
		}	
		cin>>foreign;
		getchar();
		appear[foreign]=true;
		translate[foreign]=english;
	}
	char word[11];
	while(cin>>word)
	{
		if(appear[word])
			cout<<translate[word]<<endl;
		else
			cout<<"eh"<<endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81414065