forked from summer-yiru/1024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
杨氏矩阵查找.txt
53 lines (52 loc) · 827 Bytes
/
杨氏矩阵查找.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define ROW 3
#define COL 3
int yongsearch(int arr[ROW][COL],int key){
int i = 0;
int j = COL - 1;
int tmp = arr[i][j];
while (1)
{
if (key == tmp)
{
return 1;
}
else if (tmp<key&&j>=0){
tmp = arr[i++][j];
}
else if (tmp>key&&j>=0){
tmp = arr[i][j--];
}
else {
return 0;
}
}
}
int main(){
int arr[ROW][COL] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int i = 0;
int j = 0;
int key = 0;
int ret = 0;
printf("请输入要查找的数\n");
scanf("%d", &key);
printf("数组为:\n");
for (i = 0; i < ROW;i++){
for (j = 0; j < COL;j++){
printf("%3d",arr[i][j]);
}
printf("\n");
}
ret = yongsearch(arr,key);
if (ret==1){
printf("找到了");
}
else
{
printf("找不到");
}
system("pause");
return 0;
}