这两周我们这里搞课程设计,全是算法题。出题和验题的方式就和ACM差不多。
我没有研究过ACM之类的算法,因为开始我觉得像我们这些搞Linux运维的对算法要求应该也不高吧,于是乎,上个学期的那一本课本现在还是新的。
那些题目与ACM题目比起来,简直是小儿科,但是对于我这样的算法低能来说写出来不难,修改错误就有点复杂了,半个小时写完的程序估计要4个小时才能改好。好了不说废话了。我把代码发上来,各位读者帮我看看哪里需要改进下。
题目我不发了,因为要是发了题目,其他的同学可能会通过搜索引擎找到我的代码。
代码如下:
/* 作者:恒亮+ */ #include <stdio.h> int n,m; int num[2][100000]; int sort_num(const void *a, const void *b) { if(*(int*)a>*(int*)b) return 1; else if(*(int*)a<*(int*)b) return -1; else return 0; } int find(int a) { int p_one = n-1, p_two = 0, p; while (p_two <= p_one) { p = (p_one+p_two)/2; if (a == num[0][p]) { printf("yes\n"); return 1; } if (a > num[0][p]) p_two=p+1; else p_one=p-1; } printf("no\n"); return 1; } int main() { int i,j; while (scanf("%d%d", &n, &m) != EOF) { int temp = 0; for (temp = 0; temp < n; temp++) scanf("%d", &num[0][temp]); for (temp = 0; temp < m; temp++) scanf("%d", &num[1][temp]); /*快速法排序*/ qsort((void *)num[0], n, sizeof(int), sort_num); /*二分法从num[0]中找某个数字*/ for (temp = 0; temp < m; temp++) find(num[1][temp]); } return 0; }
注意:除了排序和二分法查找,其他的地方都不能改