标签:
// 二维数组中的查找,杨氏矩阵在一个二维数组中,每行都按照从左到右的递增的顺序排序。 // 每列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个数组和一个数,判断数组中是否包含这个数 #include <stdio.h> #define col 4 #define rol 4 int yang(int(*p)[col], int num) { int i = 0; int j = col - 1; while (j+1) { int *q = &(p[i][j]); if (*q == num) return 1; else if (*q < num) { p++; } else if (*q > num) { --q; --j; } } return -1; } int main() { int arr[rol][col] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; printf("%d\n", yang(arr, 6)); printf("%d\n", yang(arr, 15)); printf("%d\n", yang(arr, 20)); printf("%d\n", yang(arr, 1)); printf("%d\n", yang(arr, 16)); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都按照从左到右的递增的顺序排序,输入这样的一个数组和一个数,判断数组中是否包含这个数
标签:
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/46775107