[jobdu]二维数组中的查找

http://ac.jobdu.com/problem.php?pid=1384 基本思路很简单,从最右上角找起。

九度的OJ做得还是不太行啊。必须要int main()才行,这道题时间卡得太紧,用cin和java都不行。

#include <stdio.h>

using namespace std;

int main()

{

    int m, n, t;

    int a[1000][1000];

    while(scanf("%d %d",&m,&n) != EOF)

    {

        scanf("%d",&t);

        for (int i = 0; i < m; i++) {

            for (int j = 0; j < n; j++) {

                scanf("%d",&a[i][j]);

            }

        }

          

        int row = 0;

        int col = n - 1;

        bool found = false;

        while (row < m && col >= 0) {

            if (a[row][col] == t) {

                found = true;

                break;

            }

            else if (a[row][col] > t) {

                col--;

            }

            else {

                row++;

            }

        }

        if (found) {

            printf("Yes\n");

        }

        else {

            printf("No\n");

        }

    }

}

/**************************************************************

    Problem: 1384

    User: lautsie

    Language: C++

    Result: Accepted

    Time:670 ms

    Memory:4856 kb

****************************************************************/

  

你可能感兴趣的:(二维数组)