#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
/*
问题:分治法问题 解决办法 使用循环 二分查找
OpenJudge 百练 2503:Babelfish
其他:
leetcode:
解决思路:学会用数据结构存储 struct cin.get()换行 cin.peek()查看元素 sort()算法 cmp()函数
时间:2021年4月4日20时52分
*/
//全局变量定义区 结构体 变量
struct Entry {
char english[11];
char foreigh[11];
}entries[100005];
//其他函数定义区
int cmp(Entry entry1,Entry entry2)
{
return strcmp(entry1.foreigh,entry2.foreigh) < 0;
}
int main()
{
//main 函数测试操作
int num = 0;
char word[11];
while (true)
{
scanf("%s%s",entries[num].english,entries[num].foreigh);
num++;
cin.get();//过滤掉换行符
if (cin.peek() == '\n') break;
}
//建立字典 foreigh进行排序
sort(entries, entries + num,cmp);
while (scanf("%s",word) != EOF)
{
int left = 0;
int right = num - 1;
int n = 0;
while (left <= right)
{
int mid = left + (right - left)/2;
n = strcmp(entries[mid].foreigh, word);
if (n < 0) left = mid + 1;
else if (n>0) right = mid - 1;
else {
printf("%s\n", entries[mid].english);
break;
}
}
if(n) printf("eh\n");
}
//system("pause");
return 0;
}
08-07
1130
03-25
03-20
632
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交