迭代:
- #include<stdio.h>
- int main(void){
- int a1 = 0;
- int a2 = 1;
- int sum = 0;
- int index;
- printf("%d ",a1);
- printf("%d ",a2);
- for(index=2;index<40;index++){
- sum = a1 + a2;
- printf("%d ",sum);
- a1 = a2;
- a2 = sum;
- }
- puts("Done");
- getchar();
- return 0;
- }
递归:
- #include<stdio.h>
- int Fbi(int number);
- int main(void){
- int index;
- int result;
- for(index=0;index<40;index++){
- result = Fbi(index);
- printf("%d ",result);
- }
- puts("Done");
- getchar();
- return 0;
- }
- int Fbi(int number){
- if(number == 0){
- return 0;
- }
- else if(number == 1){
- return 1;
- }
- else{
- return (Fbi(number-1) + Fbi(number-2));
- }
- }
转载于:https://blog.51cto.com/fanaticssk/833959