code4sk/c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第3次月考/打印杨辉三角.cpp
2023-12-15 22:38:26 +08:00

64 lines
No EOL
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
7-3 打印杨辉三角
分数 10
作者 徐镜春
单位 浙江大学
本题要求按照规定格式打印前N行杨辉三角。
输入格式:
输入在一行中给出N1≤N≤10
输出格式:
以正三角形的格式输出前N行杨辉三角。每个数字占固定4位。
输入样例:
6
输出样例:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
*/
#include <stdio.h>
int main()
{
int n, i, j;
scanf("%d", &n);
int a[100][100], cnt = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
if (j == 0 || j == i)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
for (j = 0; j <= i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
return 0;
}