题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109
题目意思:给出一个mouse-english词典,问对于输入的mouse单词,能否在这个词典里找出对应的english,不能输出“eh”
这里用到map来做,value 保存 english,key保存mouse。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 using namespace std; 8 9 const int maxn = 20 + 5; 10 char english[maxn], mouse[maxn], line[2*maxn]; 11 string value, key; 12 map<string, string> entry; 13 map<string, string>:: iterator loc; 14 15 int main() 16 { 17 while (gets(line)) 18 { 19 if (!strlen(line)) 20 break; 21 sscanf(line, "%s%s", english, mouse); // 缓冲区读入 22 value = english; 23 key = mouse; 24 entry[key] = value; 25 } 26 while (gets(line)) 27 { 28 loc = entry.find(line); // 找出保存的位置 29 if (loc != entry.end()) 30 cout << entry[line] << endl; 31 else 32 cout << "eh" << endl; 33 } 34 return 0; 35 }