斐波纳契系列用C - 的一系列数字多达给定数目的 [英] Fibonacci Series in C - The series of numbers up to a given number

查看:147
本文介绍了斐波纳契系列用C - 的一系列数字多达给定数目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能提供可能下面给出我的code的反馈?我已经做了其他语言的斐波那契数列多次,但一些奇怪的原因,它不会打印正确的级数,当我在C code它,我似乎无法找出我做错了什么。

 的#include<&stdio.h中GT;INT斐波纳契(INT N)
{
    (INT I = 0; I< N;我++)
{
如果(我== 0 ||我== 1)
{
     的printf(%d个,我);
其他
{
     的printf(%d个,((I-1)+(I-2)));
}
}
}
诠释的main()
{
   INT(* fnctPtr)(INT号);
   fnctPtr =安培;斐波纳契;
   fnctPtr(9);
   返回0;
}


解决方案

恐怕你需要真正清理以下的code.The为绝对错误使用C

 (INT I = 0; I< N;我++)

我相信你打算在循环here.But谁知道你脑子里想的是什么?那么你已经包围的孤立其他如果块斐波那契()。还有在你的程序错误众多,所以让我给你,基于如何用户想要系列多的数字(从键盘输入的选择)创建斐波纳契数列的程序。请试着去了解这个程序。这很容易背后斐波纳契数列的逻辑很简单itself.It只是很明显你是很新的C语言。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
    INT I,L,X = 1,Y = 1,Z;
    的printf(请输入你有多少个斐波那契数要\\ n);
    scanf函数(%d个,&安培; L);    的printf(斐波纳契数列%d个数字的\\ n,L);   如果(L == 1)
   {
   输出(1);
   出口(0);
   }   如果(L == 2)
   {printf的(1 + 1);
   出口(0);
   }  输出(1 + 1 +);  对于(i = 3; I< = 1;我++)
   {
   Z = X + Y;
   的printf(%D,Z);
   X = Y;
   Y = Z;
   如果(第(i + 1) - ; = 1)的printf(+);
   }
}

既然你说你实现这个系列在其他语言中,你不会有问题,开始了一系列的 0,1,1,2,3,5 ...... ,我的程序去 1,1,2,3,5 ... 的初始化既 X 1 而不是初始化 X 0 .Tweak的code来实现它。

Could anyone possibly provide feedback on my code given below? I have done the Fibonacci series multiple times in other languages, but for some odd reason, it won't print the correct series when I code it in C. I can't seem to figure out what I did wrong.

#include <stdio.h>

int fibonacci (int n)
{
    (int i = 0; i < n; i++)
{
if (i == 0 || i == 1)
{
     printf("%d,", i);
else
{
     printf("%d,", ((i-1) + (i-2)));
}
}
}


int main () 
{
   int (*fnctPtr)(int number);
   fnctPtr = &fibonacci;
   fnctPtr(9);
   return 0;
}

解决方案

I am afraid you need to really clean up your code.The following is absolutely wrong in C.

(int i = 0; i < n; i++)

I am sure you intend a for loop here.But who knows what you had in mind? Then you have enclosed a solitary else in the if block in Fibonacci().There are mistakes galore in your program, so let me give you a program that creates the Fibonacci series based on how many numbers in the series the user wants (choice entered from keyboard).Please try to understand this program.It's very easy as the logic behind Fibonacci series is simple itself.It just that it's obvious you are very new to the C language.

#include<stdio.h>
#include<stdlib.h>

int main() 
{
    int i,l,x=1,y=1,z;
    printf("Enter how many Fibonacci numbers you want\n");
    scanf("%d",&l);

    printf("The Fibonacci series of %d numbers is\n",l);

   if(l==1)
   {
   printf("1");
   exit(0);
   }

   if(l==2)
   {printf("1+1");
   exit(0);
   }    

  printf("1+1+");

  for(i=3;i<=l;i++)
   {
   z=x+y;
   printf("%d",z);    
   x=y;
   y=z;
   if((i+1)<=l) printf("+");
   }    
}

Since you say you've implemented this series in other languages, you won't have problem started the series as 0,1,1,2,3,5....., My program goes 1,1,2,3,5... as initialized both x and y to 1 instead of initializing x as 0.Tweak the code to achieve it.

这篇关于斐波纳契系列用C - 的一系列数字多达给定数目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆