杨辉三角用c语言怎么编程
#include<>
main()
{
long i,j,n,k;
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
k=1;
for(j=1;j<i;j++)
{
printf("%ld ",k);
k=k*(i-j)/j;
}
printf("1\n");
}
}
#define N 13 //定义三角最大行,可自行设定,但最大不要超过65536;
#include <>
main(){
int n,i,j;
int a[N+1][2*N+4];
printf("How Many Rows Do You Want:(Number Must Below Or Equal %d)\n",N);
scanf("%d",&n);
if(n>N){
printf("What You Input Is Big Than What We Provide!\n");
exit(1);
}
//初始化数组;
for(i=1;i<=n;i++){
for(j=-1;j<2*n+2;j++){
a[i][j]=0;
}
}
a[1][n]=1;
//设置杨辉三角的数值;
for(i=1;i<=n;i++){
for(j=1;j<2*n;j++){
if(a[i][j]!=0){
a[i+1][j-1]=a[i][j-2]+a[i][j];
a[i+1][j+1]=a[i][j+2]+a[i][j];
}
}
}
//打印杨辉三角;
for(i=1;i<=n;i++){
for(j=1;j<2*n;j++){
if(a[i][j]!=0){
printf("%3d",a[i][j]); //打印格式自设,如加“\t”或空格等;
} //主要是为了打印的图像美观;
else{
printf(" ");
}
if(j==(2*n-1)){
printf("\n");
}
}
}
}
#include <>
#include <>
void main()
{
int i,j,k,n;
static int a[50][50];
printf("Input lines\n",&n);
scanf("%d",&n);
a[1][1]=a[2][1]=a[2][2]=1;
for(i=3;i<=n;i++)
for(j=1;j<=i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=1;i<=n;i++)
{for (k=1;k<=n-i;k++)
printf(" ");
for(j=1;j<=i;j++)
printf("%2d ",a[i][j]);
printf("\n");
}
getch();
}
在80字符的屏幕上输出杨辉三角,要求1在中间,形如 1 /*第0行*/
1 1
1 2 1
1 3 3 1
1
杨辉三角 用c语言怎么编程 来自淘豆网m.daumloan.com转载请标明出处.