在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,判断数组中是否含有该整数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int Find(int t, int arr[3][3])
{
int r = 0;//行
int c = 2;//列
while (r <= 2 && c >= 0)
{
if (arr[r][c] > t)
{
c--;
}
else if (arr[r][c] < t)
{
r++;
}
else
{
return 1;
}
}
return 0;
}
int main()
{
int t;
int arr[3][3] = { 1, 2, 3, 2, 3, 4, 3, 4, 5 };
printf("输入要查找的数t:");
scanf("%d", &t);
Find(t, arr);
if (Find(t, arr) == 1)
{
printf("有数\n");
}
if (Find(t, arr) == 0)
{
printf("没有\n");
}
system("pause");
return 0;
}