The Serpentine matrix of the entry topic of ACM Programming Contest

Source: Internet
Author: User

The "Problem description" of the snake matrix is a matrix triangle arranged by a natural number starting from 1. "Require" "Data entry" the subject has multiple sets of data, each set of data consists of a positive integer n. (n not greater than 100) "Data output" for each set of data, output a serpentine matrix of n rows. Do not have an extra blank line between the two sets of outputs. The number of the same row in the matrix triangle is separated by a space. Do not leave extra spaces at the end of the line. "Sample Input" 5 "Sample Output" 1 3 6 10 152 5 9 144 8 137 1211

Code:

#include <iostream>using namespace Std;int main () {    int N;    while (cin>>n&&n>=1&&n<=100)    {        int a[n][n],i,j;        A[0][0]=1;        for (i=0; i<n; ++i)        {            a[i+1][0]=a[i][0]+i+1;        }        For (J=1, j<n; ++j)        {for            (i=0; i<n-j; ++i)            {                a[i][j]=a[i+1][j-1]+1;            }        }        For (i=0, i<n; ++i)        {for            (j=0; j<n-i; ++j)            {                if (j<n-i-1)                    cout<<a[i][j]< < "";                else                    cout<<a[i][j];            }            cout<<endl;        }    }    return 0;}

Operation Result:



Learning experience:

See the topic some wonder, is not a matrix, how to do is a triangle, but this is not the focus, can do the topic on the line;

ACM's topic is very difficult, this is what I see I think the simplest of a problem, continue to refuel.



The Serpentine matrix of the entry topic of ACM Programming Contest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.