武汉科技大学ACM:1001: 华科版C语言程序设计教程(第二版)习题6.7

Problem Description

 输出杨辉三角前n行。

Input

 输入一个数n(n <= 9)

Output

 输出杨辉三角前n行。(注意行末不能有多余的空格,数字以%3d的格式输出)

Sample Input

3
4

Sample Output

      1
    1   1
  1   2   1

        1
      1   1
    1   2   1
  1   3   3   1

HINT

 注意有多组输入。每组测试数据后面输出一个空行。

 

while(scanf("%d",&n) != EOF)

 

{

 

        ......

 

}

 

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,i,j;
 5     int a[10][10];
 6     a[0][0]=a[1][0]=a[1][1]=1;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         for(i=2;i<n;i++)
10         {
11             for(j=0;j<=i;j++)
12             {
13                 if(j==0 || i==j)
14                     a[i][j]=1;
15                 else
16                 a[i][j]=a[i-1][j-1]+a[i-1][j];
17             }
18         }
19 
20         for(i=0;i<n;i++)
21         {
22             for(int k=0;k<(n-i-1);k++)
23             {
24                 printf("  ");
25             }
26             for(j=0;j<=i;j++)
27             {
28                 if(j==0)
29                 {
30                     printf("%3d",a[i][j]);
31                 }
32                 else
33                 {
34 
35                     printf("%4d",a[i][j]);
36                 }
37                 
38                 
39             }
40             printf("\n");
41         }
42         printf("\n");
43     }
44     
45     return 1;
46 }

 

posted @ 2014-12-13 12:58  liuwt365  阅读(384)  评论(0编辑  收藏  举报