首页 > 代码库 > Practice3

Practice3

 1 #include <stdio.h> 2 #include <stdlib.h> 3  4 //void LOG_VECTOR(long long* a, int m, int n) { 5 //    int i, j; 6 //    for (i = 0; i < m; ++i) 7 //    { 8 //        for (j = 0; j < n; ++j) 9 //        {10 //            printf("%lld ",a[i * m + j]);11 //        }12 //        printf("\n");13 //    }14 //}15 //16 17 int binary_search(long long *a, int step, int length, long long  search) {18 #define STEP(i) ((i) * (step))19     int left = 0, right = length - 1;20     int mid = -1;21     while(left <= right) {22         mid = (left + right) / 2;23         if (search < a[STEP(mid)]) {24           right = mid - 1;25         } else if (search > a[STEP(mid)]) {26           left = mid + 1;27         } else {28             break;29         }30     }31     if (search == a[STEP(mid)]) {32         printf("%s", "Yes");33         exit(0);34     } else if (mid <= 0) {35         printf("%s", "No");36         exit(0);37     }38     return (a[STEP(mid)] > search) ? mid - 1 : mid;39 }40 41 42 int main() {43     int m = 0, n = 0, i =0 , j = 0;44     long long num = 0;45     int result = 0;46     long long *vector = NULL;47     scanf("%d %d", &m, &n);48     scanf("%lld", &num);49     vector = (long long*) malloc(n * m * sizeof(long long));50     if (!vector)51     {52        return -1;53     }54     while(i < m) {55         j = 0;56         while(j < n) {57            scanf("%lld", &vector[i * m + j]);58            ++j;59         }60         ++i;61     }62     //LOG_VECTOR(vector, m, n);63     result = binary_search(vector, n, m, num);64     binary_search(&vector[n * result], 1 ,n, num);65     printf("%s","No");66     return 0;67 }

 

不晓得出了什么问题 继续查

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

Practice3