对齐后的杨辉三角,c语言

输出一个杨辉三角还比较简单,让我们把这个杨辉三角对齐好
注释写的比较多,应该不需要解释了。

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int t(int n)//算出一个数有多少位,进而控制空格数
{
    int a=0;
    while(n>=1)
    {
        n=n/10;
        a++;
    }
    return a;
}
int main()
{
    int line;//总共有几行的数字
    int i,j,k,l;
    int b;
    int c;//This is the digits of each number.
    scanf("%d",&line);
    int q[100][100];
    q[0][0]=1;
    for(i=1;i<line;i++)//用i来遍历行数,i should start from line2,since there is only one number in the line 1
    {
        q[i][0]=1;//give the important condition that the first number of each line is 1
        q[i][i]=1;//let the last number of each line to be 1
        for (j=1;j<=i;j++)//j starts from the second number of each line
        {
            q[i+1][j]=q[i][j-1]+q[i][j];//add the two numbers above the number together
        }

    }
    b=t(q[line-1][(int)(line/2)]);//This is the digits of the biggest number in the triangle.
    for(i=0;i<line;i++)
    {
        for(k=0;k<(line-i);k++)
        {
            for(l=0;l<b;l++) printf(" ");//b*(line-i) spaces should be in the front of each line
        }
        for(j=0;j<=i;j++)
        {
            c=t(q[i][j]);//count the digits of each number
            for (k=0;k<b-c;k++) printf(" ");//complete the space for each number,fill it with b-c spaces
            for(k=0;k<b;k++)printf(" ");//give intervals for every two numbers
            printf("%d",q[i][j]);
        }
        printf("\n");
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝不闻道,夕不可死

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值