蛇行矩阵

蛇行矩阵

Problem
蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。 

Input
本题有多组数据,每组数据由一个正整数N组成。(N不大于100) 

Output
对于每一组数据,输出一个N行的蛇形矩阵。两组输出之间不要额外的空行。 

矩阵三角中同一行的数字用一个空格分开。行尾不要多余的空格。 

Sample Input
5

Sample Output
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11

#include<iostream>
using namespace std;

#define MAX 100
int main()
{
    int n;
    do{
        cout<<"input the number:";
        cin>>n;
    }while((n>MAX)||n<1);
    
    int m=1;
    for(int i=1;i<=n;i++)//
    {
        int k=m;
        for(int j=1;j<=n-i+1;j++)//
        {
            cout<<k<<" ";
            k=k+j+1;
        }
        cout<<endl;
        m=m+i;
    }
        
}

 

posted @ 2012-10-02 19:18  cococo点点  阅读(442)  评论(0编辑  收藏  举报