用c语言编写程序,用数组输出Fibonacci数列前20项
答案:6 悬赏:10 手机版
解决时间 2021-04-18 15:35
- 提问者网友:放下
- 2021-04-18 10:32
用c语言编写程序,用数组输出Fibonacci数列前20项
最佳答案
- 五星知识达人网友:独行浪子会拥风
- 2021-04-18 10:38
#include
void main(){
int i,n=0,f[20]={1,1};
for(i=0;i<20;i++)
{
if(i>1) f[i]=f[i-1]+f[i-2];
printf("%-5d",f[i]);
n++;
if(n%10==0)
printf("
");
}
}
void main(){
int i,n=0,f[20]={1,1};
for(i=0;i<20;i++)
{
if(i>1) f[i]=f[i-1]+f[i-2];
printf("%-5d",f[i]);
n++;
if(n%10==0)
printf("
");
}
}
全部回答
- 1楼网友:笑迎怀羞
- 2021-04-18 14:59
# include
int main ()
{
int i,n,t,a[100]={0};
scanf ("%d",&n);
t=1;
while (t<=n)
{
a[0]=1;
for (i=t-1;i>0;i--) a[i]=a[i]+a[i-1];
for (i=0;i {
printf ("%d",a[i]);
}
printf("\n");
t++;
}
system ("pause");
}
int main ()
{
int i,n,t,a[100]={0};
scanf ("%d",&n);
t=1;
while (t<=n)
{
a[0]=1;
for (i=t-1;i>0;i--) a[i]=a[i]+a[i-1];
for (i=0;i
printf ("%d",a[i]);
}
printf("\n");
t++;
}
system ("pause");
}
- 2楼网友:酒安江南
- 2021-04-18 13:31
#include
float
factorial (int i)
{
int j;
float k = 1;
for (j = 1; j <= i; j++)
k = k * j;
return (k);
}
float
combination (int i, int j)
{
float k;
k = factorial (j) / (factorial (i) * factorial (j - i));
return (k);
}
int
main (int argc, char **argv)
{
int i = 0, j, n;
while (i <= 0 || i > 16)
{
printf ("请输入要打印的行数[0-16之间]");
scanf ("%d", &i);
}
for (j = 0; j < i; j++)
{
for (n = 0; n <= j; n++)
printf ("%4.0f", combination (n, j));
printf ("\n");
}
printf ("\n\n");
return 0;
}
C:\mypro>gcc -g -Wall fibonacci2.c -o fibonacci2
C:\mypro>fibonacci2
请输入要打印的行数[0-16之间]10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
C:\mypro>
float
factorial (int i)
{
int j;
float k = 1;
for (j = 1; j <= i; j++)
k = k * j;
return (k);
}
float
combination (int i, int j)
{
float k;
k = factorial (j) / (factorial (i) * factorial (j - i));
return (k);
}
int
main (int argc, char **argv)
{
int i = 0, j, n;
while (i <= 0 || i > 16)
{
printf ("请输入要打印的行数[0-16之间]");
scanf ("%d", &i);
}
for (j = 0; j < i; j++)
{
for (n = 0; n <= j; n++)
printf ("%4.0f", combination (n, j));
printf ("\n");
}
printf ("\n\n");
return 0;
}
C:\mypro>gcc -g -Wall fibonacci2.c -o fibonacci2
C:\mypro>fibonacci2
请输入要打印的行数[0-16之间]10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
C:\mypro>
- 3楼网友:一袍清酒付
- 2021-04-18 12:39
#include
int main(){
int array[20]={1,1};
int i=2;
for(;i<20;i++)
array[i]=array[i-1]+array[i-2];
i=0;
while(i<20)
printf("%d\t",array[i++]);
}
int main(){
int array[20]={1,1};
int i=2;
for(;i<20;i++)
array[i]=array[i-1]+array[i-2];
i=0;
while(i<20)
printf("%d\t",array[i++]);
}
- 4楼网友:时间的尘埃
- 2021-04-18 11:22
#include
int main()
{
int i,k,j,l,sum=0;
i=1;
k=0;
for(j=0;j<5;j++)
{
sum+=i;
l=i;
i=i+k;
k=l;
}
printf("%d",sum);
return 0;
}
int main()
{
int i,k,j,l,sum=0;
i=1;
k=0;
for(j=0;j<5;j++)
{
sum+=i;
l=i;
i=i+k;
k=l;
}
printf("%d",sum);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯