简体   繁体   English

C程序中的斐波那契数列,其中第一个2数字由用户给出

[英]Fibonacci Series in C Program where FIRST 2 numbers are given by user

When the first number is 1 and second number is 2 , and the length is 5 , it should be 1 2 3 5 8 . 当第一个数字是1 ,第二个数字是2 ,并且长度是5 ,它应该是1 2 3 5 8 But then my output is always 1 2 1 3 4 . 但是,我的输出始终是1 2 1 3 4 I can't seem to find the problem. 我似乎找不到问题。

Another input is 2 and 5 . 另一个输入是25 Output is 2 5 1 6 7 . 输出为2 5 1 6 7 The 3rd number which is 1 shouldn't be there. 第三个数字1不应存在。 What should I change or add? 我应该更改或添加什么?

*This is already a submitted HW and yes its wrong I got the deductions already. *这已经是提交的硬件,是的,我已经扣除了错误。 Now I just want to fix this so I can study this. 现在,我只想解决此问题,以便我可以进行研究。

int main()
{
  int i, lenght = 0, fib, sum, sum1, sum2, a, b, c;
  printf("\nFirst number: ");
  scanf("%d", &a);
  printf("\nSecond number: ");
  scanf("%d", &b);
  printf("\nHow long?: ");
  scanf("%d", &lenght);

  {
    while ((a > b) || ((lenght < 2) || (lenght > 100)))
    {
      printf("\nFirst number: ");
      scanf("%d", &a);
      printf("\nSecond number: ");
      scanf("%d", &b);
      printf("\nHow long?: ");
      scanf("%d", &lenght);
    }
  }

  printf("%d\t%d\t", a, b);
  printf("%d\t", fib);
  for (i = 3; i < lenght; i++) {
    if (i <= 1) fib = i;
    else {
      a = b;
      b = fib;
      fib = a + b;
    }

    printf("%d\t", fib);
  }
}

第一次打印fib (在for循环之前),您尚未分配任何内容。

just add fib=a+b; 只需添加fib=a+b; before printing fib value. 在打印fib值之前。 Its a good coding habit to initialize all variable before using it(especially in C). 在使用变量之前初始化所有变量是一个很好的编码习惯(尤其是在C中)。

Your code seems strange to me. 您的代码对我来说似乎很奇怪。 I tried to simplify your code a bit. 我试图简化您的代码。 See this once: 看到一次:

int main()
{
    int a,b,next,last,i;
    printf("Enter the first Value:");
    scanf("%d",&a);
    printf("Enter the second Value:");
    scanf("%d",&b);
    printf("Enter the length of Fab. series:");
    scanf("%d",&last);
    printf("%d,%d,",a,b);
    for (i=3; i<= last; i++)
    {
    next = a + b;
    if(i<last)
    printf("%d,",next);
    else
    printf("%d",next);
    a = b;
    b = next;
    }
    return 0;
}

Hope it's Helpful!! 希望对您有所帮助!

Since this is for study, issues with your code: you don't need to duplicate the calls to scanf() , simply initialize one of the variables to fail (which you did: lenght = 0 ) and let the loop do its thing; 因为这是为了研究,所以代码有问题:您不需要复制对scanf()的调用,只需初始化要失败的变量之一(您这样做: lenght = 0 ),然后让循环执行它的工作; pick one indentation style and stick with it; 选择一种压痕样式并坚持下去; if you're new to C, always include the curly braces, even when the language says they're optional; 如果您是C语言的新手,则始终包括花括号,即使该语言指出它们是可选的; you (correctly) allow for a length of 2, but then print three numbers; 您(正确)允许长度为2,然后打印三个数字; your if (i <= 1) clause is a no-op as the loop starts with for (i = 3; so i is never less than 3. 您的if (i <= 1)子句是no-op,因为循环从for (i = 3;开始,所以i永远不小于3。

Putting it all together, we get something like: 放在一起,我们得到的是:

#include <stdio.h>

int main() {
    int length = 0, a, b;

    while (length < 2 || length > 100 || a > b ) {
        printf("\nFirst number: ");
        (void) scanf("%d", &a);
        printf("\nSecond number: ");
        (void) scanf("%d", &b);
        printf("\nHow long?: ");
        (void) scanf("%d", &length);
    }

    printf("%d\t%d\t", a, b);

    for (int i = 2; i < length; i++) {
        int fib = a + b;

        printf("%d\t", fib);

        a = b;
        b = fib;
    }

    printf("\n");

    return 0;
}

Note that the input error checking isn't sufficient to prevent problems. 请注意,输入错误检查不足以防止出现问题。 Eg b can be greater than a , but still mess up the sequence if you input random numbers. 例如, b可以大于a ,但是如果您输入随机数,仍然会弄乱序列。 You're assuming the user knows to put in two adjacent items from fibonacci sequence which is tricky to test. 您假设用户知道要从斐波那契数列中放入两个相邻的项目,这很难测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM