二维数组的对角搜索C++
生活随笔
收集整理的這篇文章主要介紹了
二维数组的对角搜索C++
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.需求與分析
在一個二維數組中,每一行按照從左到右遞增的順序進行排序,每一列按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否存在這個整數。
2.代碼
#include<iostream> using namespace std;bool find(int* matrix, int rows, int columns, int number) {int resTemp = false;if (matrix != NULL && rows > 0 && columns > 0){int row = 0;int column = columns - 1;while (row < rows && columns >= 0){if (matrix[row*columns + column] == number){resTemp = true;break;}else if (matrix[ row * columns + column] > number)column--;elserow++;}}return resTemp; }int main() {int nMatrix[16]= { 1, 2, 8, 9 , 2, 4, 9, 12 , 4, 7, 10, 13 , 6, 8, 11, 15 };int nNumber = 100;cout << find(nMatrix, 4, 4, nNumber) << endl;return 0; }總結
以上是生活随笔為你收集整理的二维数组的对角搜索C++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC 6.0压缩生成可执行文件的一个方法
- 下一篇: Visual Studio 调试器---