[关闭]
@chawuciren 2018-10-01T10:03:27.000000Z 字数 447 阅读 598

CS4作业4-编程题(Fibonacci数)

C语言-VS


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /*
  4. Find the nth item of the Fibonacci sequence
  5. */
  6. int Fib( int n);
  7. int main()//Main function
  8. {
  9. int result = 1,n = 1;
  10. scanf_s("%d", &n);//Enter a n
  11. result=Fib(n);//Call functions
  12. printf("%d", result);//Output result
  13. system("pause");
  14. return 0;
  15. }
  16. int Fib(int n)//Declare function
  17. {
  18. int x = 1;
  19. int y = 1;//Adjacent two
  20. int result = 0;
  21. if(n <= 2)
  22. {
  23. return 1;
  24. }
  25. for (int z = 1; z < n - 1; ++z)//Cycle count nth number
  26. {
  27. result = x + y;
  28. x = y;
  29. y = result;
  30. }
  31. return(result);
  32. }

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注