1 Star 0 Fork 0

HelloWeb / SummerOj

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2804_qs.c 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
HelloWeb 提交于 2018-07-17 13:41 . first commit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
对 nitems 对象的数组执行二分查找,base 指向进行查找的数组,key 指向要查找的元素,size 指定数组中每个元素的大小。
数组的内容应根据 compar 所对应的比较函数升序排序。
To perform the search, the function performs a series of calls to compar with key as first argument and elements of the array pointed to by base as second argument.
*/
char frn[100050][15];
char nat[100050][15];
int idx[100050];
int cnt_i = 0;
void put_elem(char *tf, char *tn) {
strcpy(frn[cnt_i], tf);
strcpy(nat[cnt_i], tn);
cnt_i++;
}
int cmpr_q(void *s1, void *s2) {
int i1 = *(int *)s1;
int i2 = *(int *)s2;
return strcmp(frn[i1], frn[i2]);
}
void init_sort() {
for (int i = 0; i < 100050; i++) {
idx[i] = i;
}
}
void sort_elem() {
qsort(idx, cnt_i, sizeof(int), cmpr_q);
}
int cmpr_b(char *tf, void *s) {
int i2 = *(int *)s;
// printf("cmpr!! %x, %d", tf, i2);
fflush(stdout);
return strcmp(tf, frn[i2]);
}
char *get_elem(char *tf) {
int *indx = bsearch(tf, idx, cnt_i, sizeof(int), cmpr_b);
if (indx == 0)
return NULL;
else
return nat[*indx];
}
int main(void) {
init_sort();
char tn[13], tf[13], buf[30], *ch;
char *ret = gets(buf);
while (ret[0] != 0) {
sscanf(ret, "%s %s", tn, tf);
put_elem(tf, tn);
ret = gets(buf);
}
// printf("%d %d", idx[0], idx[1]);
sort_elem();
// printf("%d %d", idx[0], idx[1]);
ret = gets(buf);
while (ret != NULL && ret[0] != 0) {
printf("%s\n", (ch = get_elem(ret)) ? ch : "eh" );
ret = gets(buf);
}
return 0;
}
C
1
https://gitee.com/libreliu/SummerOj.git
git@gitee.com:libreliu/SummerOj.git
libreliu
SummerOj
SummerOj
master

搜索帮助