code4sk/c/sourcecode/朱森森带领的奇妙冒险/数组2/打印杨辉三角.cpp
2023-10-18 16:17:32 +08:00

42 lines
642 B
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.

//打印杨辉三角
#include<stdio.h>
int main()
{
int arr[11][11];
int n=0;
arr[0][0]=0;
scanf("%d",&n);//输入n
for(int i=0;i<n;i++)//打印,控制外层行数循环
{
for(int k=0;k<n-i-1;k++)
{
printf(" ");//打印空格
}
//打印数据
for(int j=0;j<=i;j++)
{
if(i==j)//画图找规律在二维数组第一行为0行每行第一个最后一个为1i==j换行j=0然后根据杨辉三角规律写中间元素
{
arr[i][j]=1;
}
else if(j==0)
{
arr[i][j]=1;
}
else
{
arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
}
printf("%4d",arr[i][j]);
if(i==j)
{
printf("\n");
}
}
}
return 0;
}