zoukankan      html  css  js  c++  java
  • 弓形矩阵

    Problem Description

    输出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;
    }
    
  • 相关阅读:
    ble_app_hrs心率程序 nrf51822
    2019.05.08 《Linux驱动开发入门与实战》
    函数指针
    typedef
    回调函数
    android2
    android1
    每周总结2
    HTML
    数组(续)
  • 原文地址:https://www.cnblogs.com/yldf/p/6249920.html
Copyright © 2011-2022 走看看