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