题目描述
给定你三个葡萄牙语单词,这些词将根据下表从左到右定义一个动物。
请你确定并输出这个动物的名称。
输入格式
根据上表,输入包含三个单词,每行一个,用以识别动物,单词由小写字母构成。
输出格式
输出识别出的动物的名称。
输入样例:
vertebrado
mamifero
onivoro
输出样例:
homem
直接用if_else结构实现(引用一下y总的代码)
C++ 代码
#include <iostream>
using namespace std;
int main()
{
string a, b, c;
cin >> a >> b >> c;
if (a == "vertebrado")
{
if (b == "ave")
{
if (c == "carnivoro") cout << "aguia" << endl;
else cout << "pomba" << endl;
}
else
{
if (c == "onivoro") cout << "homem" << endl;
else cout << "vaca" << endl;
}
}
else
{
if (b == "inseto")
{
if (c == "hematofago") cout << "pulga" << endl;
else cout << "lagarta" << endl;
}
else
{
if (c == "hematofago") cout << "sanguessuga" << endl;
else cout << "minhoca" << endl;
}
}
return 0;
}
作者:yxc
链接:https://www.acwing.com/activity/content/code/content/31616/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
直接用if_else结构实现
C++ 代码
#include<iostream>
#include<string>
#include<map>
using namespace std;
void seekInMap(map<string, map<string, map<string, string>>>& m);
void printMap0(map<string, string>& map1);
void printMap1(map<string, map<string, string>>& map1);
void printMap2(map<string, map<string, map<string, string>>>& map1);
int main()
{
map<string, map<string, map<string, string>>>Animal;
seekInMap(Animal);
return 0;
}
void seekInMap(map<string, map<string, map<string, string>>>& m)
{
string key0[8] = { "carnivoro",
"onivoro",
"onivoro",
"herbivoro",
"hematofago",
"herbivoro",
"hematofago",
"onivoro" };
string value[8] = { "aguia",
"pomba",
"homem",
"vaca",
"pulga",
"lagarta",
"sanguessuga",
"minhoca",
};
string key1[4]={"ave",
"mamifero",
"inseto",
"anelideo"
};
string key2[2]={"vertebrado","invertebrado"};
map<string, map<string, string>>m1;
map<string, string>m0;
for (int i = 0; i < 2; i++)
{
for(int j=i*2;j<(i+1)*2;j++)
{
m0.insert(make_pair(key0[j].c_str(), value[j].c_str()));
// printMap0(m0);
}
m1.insert(make_pair(key1[i].c_str(),m0));
m0.clear();
// printMap1(m1);
// cout<<"\n";
}
m.insert(make_pair(key2[0],m1));
m1.clear();
// printMap2(m);
// cout<<"\n";
for (int i = 2; i < 4; i++)
{
for(int j=i*2;j<(i+1)*2;j++)
{
m0.insert(make_pair(key0[j].c_str(), value[j].c_str()));
// printMap0(m0);
}
m1.insert(make_pair(key1[i].c_str(),m0));
m0.clear();
// printMap1(m1);
// cout<<"\n";
}
m.insert(make_pair(key2[1],m1));
// printMap2(m);
string a,b,c;
cin>>a;
map<string, map<string, string>>mm1 = m[a];
cin>>b;
map<string, string>mm0 = mm1[b];
cin>>c;
cout<<mm0[c];
}
void printMap0(map<string, string>& map1)
{
for (map<string, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << "key0 = " << it->first << " \tvalue = " << it->second << "\n";
}
cout<<"\n";
}
void printMap1(map<string, map<string, string>>& map1)
{
//map<string,string>::iterator it0 = map2.begin();
//string ttt = it0->first;
for (map<string, map<string, string>>::iterator it1 = map1.begin(); it1 != map1.end(); it1++)
{
cout << "key1 = " << it1->first <<"\t";
printMap0(it1->second);
}
}
void printMap2(map<string, map<string, map<string, string>>>& map1)
{
for (map<string, map<string, map<string, string>>>::iterator it2 = map1.begin(); it2 != map1.end(); it2++)
{
cout << "key2 = " << it2->first << " \tvalue = "<< "\n";
}
}
总结:
用if_else来实现更为简洁,使用map的主要原因是刚学习完stl基础希望巩固一下,然后就写了大概8个小时的bug。
虽然自己通过此题的训练对map容器有了进一步的了解,主要是迭代器的一些使用以及对map容器的嵌套逻辑。
但是效率确实有点低了,慢慢来吧,加油!