码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-13 13:24:17      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   sp   for   on   数据   

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 }

 

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

标签:des   style   blog   io   color   sp   for   on   数据   

原文地址:http://www.cnblogs.com/liuwt365/p/4161182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!