如何为帕斯卡三角形中的数字添加括号?
问题如下:突出显示(通过放置括号)三角形中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您正在循环中打印第一个数字。您需要检查迭代是否是您想要突出显示的迭代(就像您对打印第二个数字的部分所做的那样)。
而不是:
更改为:
然后更改您最初的尝试(打印最后一位数字)以具有单个右括号。
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:
Change to:
Then change your original attempt (where you print last digits) to have a single closing parenthesis.