/*
先从右上角找起
若key小于当前值则向下查找
若key大于当前值则向左查找
*/
#include <iostream>
#include <string>#include"string.h"
using namespace std;
//m为行数,n为列数
const int n =4;
bool search(int a[][n],int key,int m){
if(key <a[0][0] || key >a[m-1][n-1]){
return false;
}
//a 为行,b为列
int c = 0;
int b = n-1;
while(c < m && b >0){
if(key==a[c][b]){
return true;
}else if(key > a[c][b]){
c++;
}else{
b--;
}
}
return false;
}
int main(){
int a[4][4] = {1,2,8,9,2,4,9,12,4,7,10,13,6,8,11,15};
cout << search(a,2,4);
}