Work nine two-dimensional array

/ * Output Triangle * /
#include<stdio.h>
#define N 14
int main(void)
{
 int i,j,k,n=0,a[N][N];
 while(n<=0||n>=13)
 {printf("输入要打印的行数:");
 scanf("%d",&n);}
 printf("%d行杨辉三角如下:\n",n);
 for(i=1;i<=n;i++)
  a[i][1]=a[i][i]=1;
 for(i=3;i<=n;i++)
  for(j=2;j<=i-1;j++)
   a[i][j]=a[i-1][j-1]+a[i-1][j];
 for(i=1;i<=n;i++){
  for(k=1;k<=n-i;k++)
    printf("   ");
  for(j=1;j<=i;j++)
   printf("%6d",a[i][j]);
    printf("\n");}
    printf("\n\n");
     return 0;
}
 
 
Experience: For solving the problem idea is to define a two-dimensional array: a [N] [N], probably greater than the number of input lines. Then the other two sides of the number of 1, i.e., when the first number of each row and the last number is the number of rows 1.n. In addition to the number of sides, any number of a sum of the two numbers. Running time can not always into isosceles triangle format, will not test it several times, and finally found a space there is no deal, carelessness.

Guess you like

Origin www.cnblogs.com/mayanyan/p/mayan9.html