题目来自剑指offer
题目
举例
分析
代码
#include <iostream>
using namespace std;
const int MAXLEN = 10;
/*arr[x][y]比num大,删除本列;arr[x][y]比num小,删除本行*/
bool Find(int arr[MAXLEN][MAXLEN],int num,int x,int y,int nLen)
{
if (x > nLen - 1 || y < 0)
{
return false;
}
if (arr[x][y] == num)
{
cout<<"x = "<<x<<" y = "<<y<<endl;
return true;
}
else if (arr[x][y] > num)
{
return Find(arr,num,x,y - 1,nLen);
}
else
{
return Find(arr,num,x + 1,y,nLen);
}
}
int main()
{
int nLen = 0;
int num = 0;
int arr[MAXLEN][MAXLEN] = {0};
cin>>nLen;//数组维度
cin>>num; //待查找的数
for (int i = 0;i < nLen;i++)//输入二维数组
{
for (int j = 0;j < nLen;j++)
{
cin>>arr[i][j];
}
}
bool bIsfind = Find(arr,num,0,nLen - 1,nLen);
if (bIsfind)
{
cout<<"isFind!"<<endl;
}
else
{
cout<<"Not Find!"<<endl;
}
system("pause");
return 1;
}