题目链接。
分析;
主要是学着用一下bsearch。
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Entry{ char english[15], foreign[15]; }entrys[100010]; int cmp(const void *a, const void *b){ return strcmp((*(struct Entry *)a).foreign, (*(struct Entry *)b).foreign); } int b_cmp(const void *a, const void *b){ return strcmp((char *)a, (*(struct Entry *)b).foreign); } int main(){ char s[30]; int top=0; struct Entry *p; while(gets(s)){ if(s[0] == '\0') break; sscanf(s, "%s %s", entrys[top].english, entrys[top].foreign); top++; } qsort(entrys, top, sizeof(entrys[0]), cmp); while(scanf("%s", s) == 1){ p = (struct Entry *)bsearch(s, entrys, top, sizeof(entrys[0]), b_cmp); if(p == NULL){ printf("eh\n"); } else printf("%s\n", p->english); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Entry{ char english[15], foreign[15]; }entrys[100010]; int cmp(const void *a, const void *b){ return strcmp((*(struct Entry *)a).foreign, (*(struct Entry *)b).foreign); } int b_cmp(const void *a, const void *b){ return strcmp((char *)a, (*(struct Entry *)b).foreign); } int main(){ char s[30]; int top=0; struct Entry *p; while(gets(s)){ if(s[0] == '\0') break; sscanf(s, "%s %s", entrys[top].english, entrys[top].foreign); top++; } qsort(entrys, top, sizeof(entrys[0]), cmp); while(scanf("%s", s) == 1){ p = (struct Entry *)bsearch(s, entrys, top, sizeof(entrys[0]), b_cmp); if(p == NULL){ printf("eh\n"); } else printf("%s\n", p->english); } return 0; }