数组
来源:互联网 发布:php截取html字符串 编辑:程序博客网 时间:2024/06/05 03:41
/*
在一个二维数组中,每一行都按照从左到右递增的顺序排序,
每一列都按照从上到下递增的顺序排序。请完成一个函数,输
入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
*/
//思想:设所查找的数位t,从右上角开始比较a[0][n-1],若a[0][n-1]>t,则说明这一列的数都大于t,故左移变成a[0][n-2],若大于则继续左移,若小于则说明这一行的数都小于t,故下移。
//直到找到这个数,或者越界则说明没有。
#include<iostream>
using namespace std;
bool myFind(int a[][4],int r,int c,int t){ //r表示行数,c表示列数,t为待寻找的数
int r0=0,c0=c-1;
while(r0<r&&c>=0){
if(a[r0][c0]==t)
return true;
else if(a[r0][c0]>t) c0--;
else r0++;
}
return false;
}
int main(){
int A[][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
bool sign;
int t;
cin>>t;
sign=myFind(A,4,4,t);
if(sign) cout<<"find!"<<endl;
else cout<<"not find!"<<endl;
return 0;
}
在一个二维数组中,每一行都按照从左到右递增的顺序排序,
每一列都按照从上到下递增的顺序排序。请完成一个函数,输
入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
*/
//思想:设所查找的数位t,从右上角开始比较a[0][n-1],若a[0][n-1]>t,则说明这一列的数都大于t,故左移变成a[0][n-2],若大于则继续左移,若小于则说明这一行的数都小于t,故下移。
//直到找到这个数,或者越界则说明没有。
#include<iostream>
using namespace std;
bool myFind(int a[][4],int r,int c,int t){ //r表示行数,c表示列数,t为待寻找的数
int r0=0,c0=c-1;
while(r0<r&&c>=0){
if(a[r0][c0]==t)
return true;
else if(a[r0][c0]>t) c0--;
else r0++;
}
return false;
}
int main(){
int A[][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
bool sign;
int t;
cin>>t;
sign=myFind(A,4,4,t);
if(sign) cout<<"find!"<<endl;
else cout<<"not find!"<<endl;
return 0;
}
0 0
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- 数组
- iOS10开发 适配Xcode8
- 自己写的 java 报数 程序
- python positional arguments 和 keyword arguments
- Android学习笔记之AndroidManifest.xml文件解析
- C++11 读写支持百万并发cache处理模型 不知道算不算。
- 数组
- 文章标题
- Android okHttp网络请求之Get/Post请求
- hibernate exception: org.hibernate.AnnotationException: No identifier specified for entity:
- px、pt、dp 和 sp
- NHibernate入门学习
- Linux 定时任务执行Shell脚本【CentOS6.8】
- Mac Android Studio NDK环境
- MyBatis Spring整合配置映射接口类与映射xml文件