<算法编程> 给定二维数组查找一个数是否存在在这个数组中

 1 #include<iostream>
 2 using namespace std;
 3 
 4 bool FindNum(int arr[][5],int rows,int cols,int num)
 5 {
 6     if(arr == NULL || rows <= 0 || cols <= 0) return false;
 7 
 8     for(int row=rows-1,col=0;row>=0&&col<cols;)
 9     {
10         if(num < arr[row][col])
11         {
12             row --;
13             continue;
14         }
15         if(num > arr[row][col])
16         {
17             col ++;
18             continue;
19         }
20         if(num == arr[row][col])
21             return true;
22     }
23     return false;
24 }
25 
26 int main()
27 {
28     int arr[5][5] = {{2,4,5,6,7},\
29                      {5,8,13,17,22},\
30                      {7,9,24,26,30},\
31                      {11,18,27,31,45},\
32                      {19,20,28,32,50}};
33     int num;
34     cin >> num;
35     if(FindNum(arr,5,5,num))
36         cout << "find success!" << endl;
37     else 
38         cout << "find failed!" << endl;
39     
40     system("pause");
41     return 0;
42 }

 

posted @ 2019-07-20 18:53  Aaaaaalei  阅读(276)  评论(0编辑  收藏  举报