杨辉三角

    这是杭电hdu上杨辉三角的链接http://acm.hdu.edu.cn/showproblem.php?pid=2032

 Problem Description

  还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
  1
  1 1
  1 2 1
  1 3 3 1
  1 4 6 4 1
  1 5 10 10 5 1
 
 Input
  输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层  数。
 
 Output:
  对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空  行。
 
 Sample Input
  2 3
 
 Sample Output
  1
  1 1
 
  1
  1 1
  1 2 1
 
  值得注意题目中的空格和换行字眼-----有时候答案是对的,但呈现格式不对,杭电提示Wrong Answer,而不是提示Presentation Error.

  具体参见下面代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define N 32
 5 
 6 using namespace std;
 7 
 8 int a[N][N];
 9 int n;
10 void creat() {
11     for(int i = 0;i <= n; ++i) {
12         a[i][i] = 1;
13         a[i][0] = 1;
14     }
15     for(int i = 2;i <= n; ++i) {
16         for(int j = 1;j < i; ++j) {
17             a[i][j] = a[i-1][j] + a[i-1][j-1];
18         }
19     }
20 }
21 void print_ () {
22     int i,j;
23     for(i = 0;i < n; ++i) {
24         for(j = 0;j < i; ++j) {
25             cout << a[i][j] << " ";
26         }
27         cout << a[i][j];/////根据题意--每个整数之间用空格隔开,在这里单独输出最后一个整数1即可
28         cout << endl;
29     }
30     cout << endl;
31 }
32 int main() {
33     memset(a,0,sizeof(a));
34     while (cin >> n) {
35         creat();
36         print_();
37     }
38     return 0;
39 }
View Code
  希望和各位码友一起成长,欢迎评论。