剑指Offer之1384:二维数组中的查找
2013-10-12 11:26
302 查看
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http://ac.jobdu.com/problem.php?pid=1384 * 结果:AC * 来源:剑指Offer * 总结: **********************************/ #include<stdio.h> #define N 1001 int Matrix ; int Find(int Matrix ,int m,int n,int t){ //从矩阵的右上角查起 int row = 0; int col = n - 1; while(row < m && col >= 0){ int number = Matrix[row][col]; //如果该数字等于要查找的数据,查找过程结束 if(number == t){ return 1; } //如果该数字小于要查找的数据,查找的数据肯定在该数据的下方,即剔除这个数据所在的行 else if(number < t){ row++; } //如果该数字大于要查找的数据,查找的数据肯定在该数据的左方,即剔除这个数据所在的列 else{ col--; } } return 0; } int main(){ int m,n,t,i,j; while(scanf("%d %d",&m,&n) != EOF){ scanf("%d",&t); //输入矩阵 for(i = 0;i < m;i++){ for(j = 0;j < n;j++){ scanf("%d",&Matrix[i][j]); } } //查找数据 int result = Find(Matrix,m,n,t); if(result == 1){ printf("Yes\n"); } else{ printf("No\n"); } } return 0; }
【分析】
【第二种方法】
此题考查的主要是第一种方法的解题思路。
#include<stdio.h> int main() { int i,j,m,n,t,number,flag; while (scanf("%d %d",&m, &n) != EOF) { flag = 0; scanf("%d",&t); for(i = 0; i < m; i++) for(j = 0; j < n; j++) { scanf("%d",&number); if(t == number) { flag = 1; } } if(flag == 1) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
相关文章推荐
- 九度Online Judge | 剑指offer | 题目1384:二维数组中的查找
- 【剑指Offer面试题】九度OJ1384:二维数组中的查找
- 剑指OFFER之二维数组中的查找(九度OJ1384)
- 剑指offer第3题 二维数组的查找 九度OJ 1384
- 【剑指Offer面试编程题】题目1384:二维数组中的查找--九度OJ
- 【剑指Offer面试题】九度OJ1384:二维数组中的查找
- 九度oj-剑指offer-题目1384:二维数组中的查找
- 【剑指Offer面试编程题】题目1384:二维数组中的查找--九度OJ
- 剑指OFFER之二维数组中的查找(九度OJ1384)
- [剑指offer-1384] 二维数组中的查找
- [九度OnlineJudge][剑指Offer]题目1384:二维数组中的查找
- 题目1384:二维数组中的查找(剑指offer-面试题3)
- 【剑指Offer面试编程题】题目1384:二维数组中的查找--九度OJ
- 【剑指offer】二维数组中的查找
- 剑指offer-二维数组中的查找(1)
- 剑指offer-面试题3 二维数组中的查找
- 剑指Offer——二维数组中的查找
- [剑指Offer]二维数组中的查找
- 重新解读剑指Offer之3题 二维数组中的查找
- 剑指offer面试题3二维数组的查找