【获取斐波那契数列中的第k个数】
一、递归
#include<stdio.h> int febei(int k){ if( k == 1 || k == 2) return 1; else return febei(k-1)+febei(k-2); } int main(){ int k; scanf("%d",&k); int x = febei(k); printf("%d",x); return 0; }
二、for循环
#include<stdio.h> int main(){ int k; int x,x1 = 1,x2 = 1; scanf("%d",&k); for(int i = 1;i<=k;i++){ if(i == 1 || i == 2) x = 1; else{ x = x1 + x2; x1 = x2; x2 = x; } } printf("%d",x); return 0; }