题目:
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
基本思想:
首先选取数组中右上角的数字。如果=要找的数字,结束。如果大于要找的数字,剔除这个数字所在的列;如果小于要找的数字,剔除这个数字所在的行。
#include <iostream>
using namespace std;
void find(int a[][4],int rows,int columns,int number)
{
if(rows > 0 && columns > 0)
{
int row = 0,column = columns -1;
while(row < rows && column >=0)
{
if(a[row][column] == number)
{
cout<<row<<" "<<column<<endl;
break;
}
else if(a[row][column] > number)
--column;
else
++row;
}
}
}
void main()
{
int a[4][4]={
1,2,8,9,
2,4,9,12,
4,7,10,13,
6,8,11,15};
find(a,4,4,7);
}
原文地址:http://blog.csdn.net/wtyvhreal/article/details/45219263