#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*
分析特性,得到规律如下:
要查找的数值如果比右上角的值大,那么它将大于整个行;
要查找的数值比如果右上角的值小,那么它将小于整个列。
如果相等的话,查找就结束了*/
/*
* 参数1:矩阵数组
* 参数2:行
* 参数3:列
* 参数4:要查找的元素*/
bool Find(int* matrix, int rows, int columns, int number)
{
bool found = false;
if(matrix != NULL && rows > 0 && columns > 0)
{
int x = 0;
int y = columns - 1;
while(x < rows && y >=0)
{
/*右上角的那个数组的表示方法*/
if(matrix[x * columns + y] == number)
{
found = true;
/*出错点!*/
break;
}
else if(matrix[x * columns + y] > number)
y--;
else
x++;
}
}
return found;
}
int main()
{
int sz[] = { 1, 2, 3,
4, 5, 6,
7, 8, 9 };
int b = Find(sz,3,3,5);
printf("resultl = %d\n",b);
return 0;
}
二维数组的查找(C语言)
最新推荐文章于 2022-09-27 23:29:42 发布