弓形矩阵 Problem Description Input Output Sample Input Sample Output 源代码

输出n*m的弓型矩阵

Input

多组测试数据
每组输入2个整数 n和m(不大于20)

Output

输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个位置,靠右)

Sample Input

4 3

Sample Output

1 2 3
6 5 4
7 8 9
12 11 10

源代码

#include <iostream>
#include<iomanip>
using namespace std;

int main()
{
    int row,col;
    int k=-1;
    int *a=NULL;
    int i,j;
    while(cin>>row>>col)
    {
        k=0;
        if(row>20 || col>20)
        {
            return 0;
        }

        a=new int[row*col];

        for(i=0;i<row;++i)
        {
            if(i%2==0)
            {
                 for(j=0;j<col;++j)
                 {
                     a[i*col+j]=++k;
                 }

            }
            else
            {
                  for(j=col-1;j>=0;--j)
                 {
                     a[i*col+j]=++k;
                 }
            }

        }
        for(i=0;i<row;++i)
        {
            for(j=0;j<col-1;++j)
            {
                cout<<setw(2)<<a[i*col+j]<<" ";
            }
            cout<<setw(2)<<a[i*col+j]<<endl;
        }



    }
    return 0;
}