链接:点击打开链接
题意:输出斐波那契的前四位数
代码:
#include <queue> #include <cstdio> #include <cstring> #include <algorithm> #include <stdlib.h> #include <iostream> #include <math.h> using namespace std; int a[30]; int main(){ int i,j,n; double temp=(sqrt(5)+1)/2,t; a[0]=0;a[1]=1; for(i=2;i<=20;i++) a[i]=a[i-1]+a[i-2]; while(scanf("%d",&n)!=EOF){ if(n<=20){ cout<<a[n]<<endl; continue; } //前四位推导公式: t=n*log10(temp)-0.5*log10(5.0); //s=d.xxx*10^(len-4) t-=floor(t); //log10(s)=log10(d.xxxxx)+log10(10^(len-4))=log10(d.xxxx)+len-4 t+=3; //log10(s)+4-len=log10(d.xxxx) t=pow(10,t); //d.xxxx=10^(log10(s)+4-len) while(t<1000) //s=(1/sqrt(5))*[(1+sqrt(5))/2.0]^i t*=10; //len=(int)log10(s)+1 printf("%d ",(int)t); //d.xxxx=10^(log10(s)+4-((int)log10(s)+1))=10^(log10(s)-(int)log10(s)+3) } return 0; }