hdu1568

链接:点击打开链接

题意:输出斐波那契的前四位数

代码:

#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\n",(int)t);          //d.xxxx=10^(log10(s)+4-((int)log10(s)+1))=10^(log10(s)-(int)log10(s)+3)
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值