博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二位数组中的查找
阅读量:2722 次
发布时间:2019-05-13

本文共 851 字,大约阅读时间需要 2 分钟。

题目:

给一个二维数组每一行的数字是从左到右是递增的,每一列的数字从上到下是递增的,查找一个数是否在这个二维数组中存在存在。

#define  _CRT_SECURE_NO_WARNINGS#include
#include
using namespace std;bool Find(int* arr,int rows,int cols,int num){ assert(arr); assert(rows > 0); assert(cols > 0); int row = 0; int col = cols-1; while (row < rows && col >= 0) { if (arr[row*cols + col] == num)//从右上角开始找,如果小了就往左,大了往下找 { return true; } else { if (arr[row*cols + col]>num) { --col; } else { ++row; } } } return false;}void test(){ int arr[][4] = { { 1, 2, 8, 9 }, { 2, 4, 9, 10 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } }; int ret=Find((int*)arr, 4, 4, 6); cout << ret;}int main(){ test(); system("pause"); return 0;}

转载地址:http://uqstd.baihongyu.com/

你可能感兴趣的文章
GoldenGate进程 abend,报错为OGG-00868 ORA-02396: Exceeded Maximum Idle Time, Please Connect Again
查看>>
【翻译自mos文章】即使resource_limit = false, password的 资源限制也会生效
查看>>
【翻译自mos文章】对dba_users视图中,account_status列的解释
查看>>
【翻译自mos文章】开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法
查看>>
[转]使用 watchdog 构建高可用性的 Linux 系统及应用
查看>>
【转】How to check HBA host and its corresponding WWPN on RHEL 5, 6 or 7?
查看>>
【转】How can I check the port status of my fibre channel HBA?
查看>>
【转】rhel内核版本和HBA卡驱动版本之间的对应关系
查看>>
【转】How can I get firmware version of a Qlogic HBA card?
查看>>
大数据开发工程师面试主要面试哪些内容
查看>>
Java 抽象类是什么
查看>>
JavaScript中Nodejs中事件循环和js 事件循环差异
查看>>
什么是双亲委派模型?
查看>>
Vue的生命周期的钩子函数?
查看>>
hadoop中combiner的作用是什么?
查看>>
spark streaming和flink的区别
查看>>
ThinkPHP5.0的think-swoole 开启WebSocket的SSL支持 使用wss连接
查看>>
Python环境下对于字符串排序的方式
查看>>
python-csv文件删除行或者删除列
查看>>
深度压缩技术总结
查看>>