新类型矩阵:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
查找一个指定数据是否在矩阵中
#include <iostream>
using namespace std;
bool Find(int* matrix, int row, int colum, int num);
int main()
{
int matrixOne[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
int* matrixTwo = NULL;
bool bl =Find(matrixOne, 3, 4,-3);
if (bl)
{
cout<<"find it"<<endl;
}
else
cout<<"cann‘t find it"<<endl;
return 0;
}
bool Find(int* matrix, int row, int colum, int num)
{
/////左下角
bool fund = false;
if ((matrix == NULL) || (row <=0 )|| (colum<=0))
{
return fund;
}
if (num < matrix[0] || num >matrix[row*colum -1])
{
return fund;
}
int r = row -1;
int col = 0;
while ((r >=0) &&(col < colum))
{
if (matrix[r*colum +col] == num)
{
fund = true;
cout<<"row = "<< r<<endl;
cout<<"colum =" <<col<<endl;
return fund;
}
else if (matrix[r*colum +col] < num)
col++;
else
r--;
}
return fund;
}