二分贪心--02

题目大概:

输入一个一对一的字典,每行两个字符串,然后输入一个单词,输出翻译后的单词,如果字典中没有该单词,输出eh。

思路:

输入字典用sscanf()函数,分别放在一个结构体中,对结构体排序,然后运用二分查找寻找要翻译的单词。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stdio.h>
#include <string.h>
using namespace std;
struct f
{
char s1[20],s2[20];
} a[100001];

int cmp(f a,f b)
{return strcmp(a.s2,b.s2)<0;
}

int main()
{int le;
    le=0;
char b[50];
while(gets(b))
{
if(b[0]=='\0')break;
sscanf(b,"%s%s",a[le].s1,a[le].s2);
le++;
}
sort(a,a+le,cmp);
while(gets(b))
{
int l=0,r=le-1,k=1;
while(l<=r)
{
    int mid=(l+r)/2;
    if(strcmp(b,a[mid].s2)==0)
    {printf("%s\n",a[mid].s1);k=0;break;}
else if(strcmp(b,a[mid].s2)<0)r=mid-1;
else l=mid+1;
}
if(k)
printf("eh\n");
}

    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值