NOI 21:二维数组右上左下遍历(模拟)

  描述

给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按从左上到右下的对角线顺序遍历整个数组。


输入输入的第一行上有两个整数,依次为row和col。
余下有row行,每行包含col个整数,构成一个二维整数数组。
(注:输入的row和col保证0 < row < 100, 0 < col < 100)输出按遍历顺序输出每个整数。每个整数占一行。样例输入
3 4
1 2 4 7
3 5 8 10
6 9 11 12
样例输出
1
2
3
4
5
6
7
8
9
10
11
12

#include<string>
#include<cstdio>
using namespace std;

int num[105][105];
int row,col;

int main()
{
    scanf("%d%d",&row,&col);


    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            scanf("%d",&num[i][j]);
        }
    }

    int x=0;
    int y=0;
    int m=0;
    int n=0;
    for(int i=0;i<row*col;i++)
    {
        printf("%d\n",num[x][y]);
        if((y==0||x==row-1)&&(n+1)<col)
        {
            y=++n;
            x=0;
            continue;
        }
        if((x==row-1||y==0)&&m<row)
        {
             x=++m;
             y=col-1;
             continue;
        }

        y=y-1;
        x=x+1;
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/shanwenkang/article/details/81038368