找出规律:f[i][j] = f[i-1][j]+f[i-1][j-1],注意边界问题,防止越界访问
#include <stdio.h>
#include <string.h>
#define ONLINE_JUDGE
int
main(int argc, char **argv) {
int n, i, j;
int triangle[31][31];
//int is_first = 1;
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
memset(triangle, 0, sizeof(triangle));
triangle[1][1] = 1;
for (i = 2; i <= 30; ++ i)
for (j = 1; j <= i; ++ j)
triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
while (scanf("%d", &n) == 1) {
//if (!is_first) printf("\n");
for (i = 1; i <= n; ++ i) {
for (j = 1; j <= i - 1; ++ j) {
printf("%d ", triangle[i][j]);
}
printf("%d\n", triangle[i][i]);
}
printf("\n");
//is_first = 0;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif // ONLINE_JUDGE
return 0;
}