Guest User

Untitled

a guest
Jun 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. /* 用迭代寫 費氏數列 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /*
  6. Fibonacci
  7. f1 f2
  8. 0 1
  9. 1 2
  10. 3 5
  11. 8 13
  12. 21 34
  13.  
  14. f1+f2=f3
  15. */
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. int f1=0, f2=1;
  20. int input=40;
  21.  
  22. for (int i = 1; i < input/2; ++i)
  23. {
  24. f1 = f1+f2;
  25. f2 = f2+f1;
  26. }
  27.  
  28. if (input %2 ==1)
  29. {
  30. printf("%d\n", f1);
  31. }
  32. else
  33. {
  34. printf("%d\n", f2);
  35. }
  36.  
  37.  
  38. return 0;
  39. }
Add Comment
Please, Sign In to add comment