1.杨氏矩阵 有一个二维数组.数组的每行从左到右是递增的,每列从上到下是递增的.在这样的数组中查找一个数字是否存在。时间复杂度小于O(N);
数组: 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define ROW 4
#define COL 4
int Yang(int arr[ROW][COL], int val)
{
int i = 0;
int j = COL - 1;
int tmp = arr[i][j];
while (1){
if (tmp == val){
return 1;
}
else if (tmp < val && j >= 0){
tmp = arr[++i][j];
}
else if (tmp > val &&j >= 0){
tmp = arr[i][--j];
}
else{
return 0;
}
}
}
int main()
{
int a[ROW][COL] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
int i = 0;
int j = 0;
int num;
printf("数组为:\n");
for (i = 0; i < ROW; i++){
for (j = 0; j < COL; j++){
printf("%5d", a[i][j]);
}
printf("\n");
}
printf("Please Enter:");
scanf("%d", &num);
if (Yang(a, num)){
printf("%d在该数组中\n", num);
}
else{
printf("%d不在该数组中\n", num);
}
system("pause");
return 0;
}