【发布时间】:2015-10-24 08:55:51
【问题描述】:
我知道了如何构造帕斯卡三角形和代码 下面是完美的,但是......在这段代码中,我让 1 出现在 第一行通过创建一个新的 for 循环特别是它......有没有 在不使用独占 for 循环的情况下生成帕斯卡三角形的方法 让 1 首先出现...非常感谢您的帮助:)
//pascal triangle with ncr function
#include <stdio.h>
#include <conio.h>
int ncr(int i,int j);
int main()
{
int i,j,v,n,f,s;
printf("Enter the number of rows required\n");
scanf("%d",&n);
f=n;
//this is what i am exclusively using for printing 1 in 1st row
for(;f>0;f--)
{
printf(" ");
}
printf("1\n");
//is there a way to generate the above 1 using only the below for loop
for(i=1;i<=n;i++)
{
for(s=n-i;s>0;s--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
v=ncr(i,j);
printf("%d ",v);
}
printf("\n");
}
}
int ncr(int i,int j)
{
int k;
float ans=1;
for(;j>=1;j--)
{
ans=((ans*i)/j);
i--;
}
k=ans;
return(k);
}
【问题讨论】:
-
为什么您的
ncr实现看起来像在main方法内? -
啊!我明白了......你说得对,我已经在 main 函数中实现了它......谢谢@Arc676
-
和
int ncr(int i,int j); {删除; -
你检查过...我知道如何完美地构建它,但我使用排他循环在第一行生成 1...有没有办法改变它
标签: c function loops logic pascals-triangle