zoukankan      html  css  js  c++  java
  • 武汉科技大学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("
    ");
    41         }
    42         printf("
    ");
    43     }
    44     
    45     return 1;
    46 }
  • 相关阅读:
    第二次作业
    大学——新生活方式
    第四次作业
    第三次作业
    第二次作业——起航
    梦开始的地方
    第四次作业
    第三次作业
    第二次作业
    博客作业 随笔
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4161182.html
Copyright © 2011-2022 走看看