在杨氏矩阵中找数字,存在返回下标,不存在返回“不存在”,杨氏矩阵:每行每列的数字都是递增排序的
程序员文章站
2024-02-24 17:38:31
...
#include <stdio.h>
#include <windows.h>
void search_number(int arr[][4],int num)
{
int i = 0;
int j = 3;
int count = 0;
while (i<=3&&j>=0)
{
if (num == arr[i][j])
{
printf("下标为:x: %d y: %d\n", i, j);
i++;
j--;
count++;
}
else if (num < arr[i][j])
{
j--;
}
else
{
i++;
}
}
if (count==0)
printf("不存在!");
}
int main()
{
int arr[4][4] = { {1,2,3,4}, {2,3,4,5}, {3,4,5,6}, {4,5,6,7} };
int num = 0;
scanf("%d",&num);
search_number(arr,num);
system("pause");
return 0;
}