程序员代码面试指南: 数学计算 在行列都排好序的矩阵中找指定的数

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,k;
    cin >> n >> m >> k;
    int a[n][m];
    for(int i = 0;i < n;++i)
        for(int j = 0;j < m; ++j)
        {
           cin >> a[i][j];
        }
    int x ;
    int col = m - 1;
    int row = 0;
    while(row < n && col > -1)
    {
        if(k == a[row][col])
        {
            cout  << "Yes"<< endl;
            return 0;
        }
        else if(k > a[row][col])
            row++;
        else 
            col--;
    }
    cout << "No" << endl;
}

 

posted @ 2019-10-14 10:06  冷眼旁观你的泪  阅读(168)  评论(0编辑  收藏  举报