如何为帕斯卡三角形中的数字添加括号?

发布于 2024-10-17 14:09:57 字数 1758 浏览 2 评论 0原文

问题如下:突出显示(通过放置括号)三角形中 C(n, k) 的答案。 我已经为 11 线帕斯卡三角形构建了代码。但我无法突出显示 C(n, k)。

这就是我所做的。

#include<stdio.h>

int recursive(int n, int k);

int main(void)
{
    int row, space, column, b, x, y;

    printf("Pascal triangle with 11 rows : \n");
    printf("Enter the row and column that you would like to highlight on the triangle : ");
    scanf("%d %d", &x, &y);

    if (x > 11)
    {
          printf("Error! The row entered must be smaller than 11! \n ");
    }

    else if (y > x)
    {
        printf("Error! Column CANNOT more than number of row!\n");
    }

    else 
    {
        for(row = 0 ; row < 11 ; row++)
        {
            for(space = 30 - 3 * row ; space > 0 ; space--)
            {
                printf(" ");
            }

            for(column = 0;column <= row; column++)
            {
                {
                if(column == 0 || row == 0)
                {
                    b = 1;
                }
                else
                {
                    b = ( b * (row - column + 1)/column );
                }
                printf("%6d", b);
                }

                if(row == x-1 && column == y-1)
                {
                    printf("(%2d)",recursive( row , column ));
                }
                else
                {
                    printf("%d ",recursive( row , column ));
                } 
            }
            printf("\n");

        }

    }

    return 0;
}

int recursive(int n,int k)
{
    if( k == 0 || k > n)
        return 0;
    else if( n == 1 && k == 1 )
        return 1;
    else
        return ( recursive( n - 1, k) + recursive( n - 1, k - 1) );
}

有人可以告诉我我哪里做错了吗?

Here's the question : highlight(by putting a bracket) the answer for C(n, k) in the triangle.
I have constructed my code for my 11 line pascal triangle. but i can't be able to highlight C(n, k).

Here's what I've done.

#include<stdio.h>

int recursive(int n, int k);

int main(void)
{
    int row, space, column, b, x, y;

    printf("Pascal triangle with 11 rows : \n");
    printf("Enter the row and column that you would like to highlight on the triangle : ");
    scanf("%d %d", &x, &y);

    if (x > 11)
    {
          printf("Error! The row entered must be smaller than 11! \n ");
    }

    else if (y > x)
    {
        printf("Error! Column CANNOT more than number of row!\n");
    }

    else 
    {
        for(row = 0 ; row < 11 ; row++)
        {
            for(space = 30 - 3 * row ; space > 0 ; space--)
            {
                printf(" ");
            }

            for(column = 0;column <= row; column++)
            {
                {
                if(column == 0 || row == 0)
                {
                    b = 1;
                }
                else
                {
                    b = ( b * (row - column + 1)/column );
                }
                printf("%6d", b);
                }

                if(row == x-1 && column == y-1)
                {
                    printf("(%2d)",recursive( row , column ));
                }
                else
                {
                    printf("%d ",recursive( row , column ));
                } 
            }
            printf("\n");

        }

    }

    return 0;
}

int recursive(int n,int k)
{
    if( k == 0 || k > n)
        return 0;
    else if( n == 1 && k == 1 )
        return 1;
    else
        return ( recursive( n - 1, k) + recursive( n - 1, k - 1) );
}

Can someone tell me where have I done wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-10-24 14:09:57

因为您正在循环中打印第一个数字。您需要检查迭代是否是您想要突出显示的迭代(就像您对打印第二个数字的部分所做的那样)。

而不是:

printf("%6d", b);

更改为:

if(row == x-1 && column == y-1){
    printf("(6%d", b);
}
else{
    printf("%6d", b);
}

然后更改您最初的尝试(打印最后一位数字)以具有单个右括号。

printf("%2d)",recursive( row , column ));

Since you're printing the first digit in a loop. You need to check if the iteration is the one you want to highlight (just like you did for the part where you print the second digit).

Instead of:

printf("%6d", b);

Change to:

if(row == x-1 && column == y-1){
    printf("(6%d", b);
}
else{
    printf("%6d", b);
}

Then change your original attempt (where you print last digits) to have a single closing parenthesis.

printf("%2d)",recursive( row , column ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文