博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
题目1384:二维数组中的查找
阅读量:4645 次
发布时间:2019-06-09

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

题目描述:

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

 

输入:

输入可能包含多个测试样例,对于每个测试案例,

输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的矩阵的行数和列数。

输入的第二行包括一个整数t(1<=t<=1000000):代表要查找的数字。

接下来的m行,每行有n个数,代表题目所给出的m行n列的矩阵(矩阵如题目描述所示,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。

 

输出:

对应每个测试案例,

输出”Yes”代表在二维数组中找到了数字t。

输出”No”代表在二维数组中没有找到数字t。

 

样例输入:
3 351 2 34 5 67 8 93 312 3 45 6 78 9 103 3122 3 45 6 78 9 10
样例输出:
YesNoNo
1 #include 
//1384 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 21 using namespace std;22 23 int martix[1001*1001];24 25 int M;26 int N;27 28 bool binarysearch(int t ,int begin, int end )29 {30 int left = begin;31 int right = end; 32 while( left<= right )33 {34 int mid = (left+right)/2;35 if( t == martix[mid] )36 {37 return true;38 }39 else if( t > martix[mid] )40 {41 left = mid+1;42 }43 else44 {45 right = mid-1;46 }47 }48 return false;49 }50 51 int main()52 {53 int m;54 int n;55 int t;56 int i;57 int j;58 59 while( scanf("%d%d",&m,&n) != EOF)60 {61 M = m;62 N = n;63 memset(martix,0,sizeof(martix));64 scanf("%d",&t);65 for(i = 0 ;i < m; i++)66 for(j = 0; j < n; j++)67 scanf("%d",&martix[n*i+j]);68 69 if(!binarysearch(t,0,m*n-1))70 printf("No\n");71 else72 printf("Yes\n");73 }74 75 return 0;76 }

 

 

转载于:https://www.cnblogs.com/chchche/p/3466025.html

你可能感兴趣的文章
SVN使用指南
查看>>
【转载】掌 握 3 C ‧ 迎 接 亮 丽 职 涯
查看>>
爬取网站附件
查看>>
java基础图形界面和IO系统
查看>>
javascript学习笔记
查看>>
hdu 3996
查看>>
python第三十九课——面向对象(二)之初始化属性
查看>>
python学习笔记之函数装饰器
查看>>
FEM计算2D瞬态热传导方程
查看>>
四年时光,匆匆而过
查看>>
【php】【psr】psr1 基础编码规范
查看>>
WAF SSI
查看>>
LDAP & it's implementation
查看>>
Apache HttpComponents中的cookie匹配策略
查看>>
冰封的海盗攻略
查看>>
Netty4.x中文教程系列(四) 对象传输
查看>>
linux下find命令使用举例、
查看>>
GET请求在Tomcat中的传递及URI传递
查看>>
ubuntun 服务器与Mac
查看>>
重温JSP学习笔记--与日期数字格式化有关的jstl标签库
查看>>