判断一个数是否在一个二维数组中

来源:互联网 发布:godaddy建站 编辑:程序博客网 时间:2024/05/21 02:40

这里写图片描述
分析参考网络

#include<iostream>  using namespace std;  #include<assert.h>  const int M = 3;  const int N = 3;  bool Find(const int *a, int x, int y,int &key)  {      assert(a);      while (x < M&&x >= 0 && y < N&&y >= 0)      {          int num = a[x*M + y];          if (key>num)//如果查找的数字大于第一行最后一个数字,则x++;          {              x++;          }          else if(key<num)//如果查找的数字小于某一行最后一个数字,则y--;          {              y--;          }          else          {              return true;          }      }      return false;  }  void Judge(int ret, int key)  {      if (ret == 1)      {          cout << "数字" << key << "找到了!" << endl;      }      else//if (ret=0)      {          cout << "数字" << key << "没有找到!" << endl;      }  }  int main()  {      int a[9] = { 1, 2, 3,                    4, 5, 6,                   7, 8, 9 };      int num = 0;      cout << "请输入要查找的数:" << endl;      cin >> num;      int ret = Find(a, 0, M - 1, num);      Judge(ret,num);      system("pause");      return 0;  }  
阅读全文
0 0
原创粉丝点击