【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都依照从左到右的递增的顺序排序,输入这种一个数组和一个数,推断数组中是否包括这个数

//  二维数组中的查找,杨氏矩阵在一个二维数组中。每行都依照从左到右的递增的顺序排序。

// 每列都依照从上到下递增的顺序排序。请完毕一个函数,输入这种一个数组和一个数。推断数组中是否包括这个数 #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 ", yang(arr, 6)); printf("%d ", yang(arr, 15)); printf("%d ", yang(arr, 20)); printf("%d ", yang(arr, 1)); printf("%d ", yang(arr, 16)); return 0; }








原文地址:https://www.cnblogs.com/yutingliuyl/p/6803492.html