zoukankan      html  css  js  c++  java
  • hdu 1250 Hat's Fibonacci

    Problem Description
    A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
    F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
    Your task is to take a number as input, and print that Fibonacci number.
     
    Input
    Each line will contain an integers. Process to end of file.
     
    Output
    For each case, output the result in a line.
     
    Sample Input
    100
     
    Sample Output
    4203968145672990846840663646
    Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
    高精度加法,用结构体实现,先打表,节省时间,代码如下:
    #include<stdio.h>
    struct bignum{
        int s[710];
        int l;
    }f[8011];
    int m=10000;
    bignum operator+(bignum a,bignum b)//a=a+b
    {
        int i,d=0;
        if(b.l<a.l)
            b.l=a.l;
        for(i=0;i<b.l;i++)
        {
            b.s[i]+=d+a.s[i];
            d=b.s[i]/m;
            b.s[i]%=m;
        }
        if(d) b.s[b.l]=d,b.l++;
        return b;
    }
    void print(bignum a)//打印大数据的数组
    {
        printf("%d",a.s[a.l-1]);
        for(int i=a.l-2;i>=0;i--)
            printf("%04d",a.s[i]);
        printf("
    ");
    }
    int main()
    {
        int i,n;
        f[1].s[0]=f[2].s[0]=f[3].s[0]=f[4].s[0]=1;
        f[1].l=f[2].l=f[3].l=f[4].l=1;
        for(i=5;i<=8000;i++)
            f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4];
        while(scanf("%d",&n)!=EOF)
        {
            print(f[n]);
        }
        return 0;
    }
        
     
  • 相关阅读:
    C语言实现用户输入
    QQ头像一键添加校徽
    csu oj Infected Computer 1427
    质数个数
    stl实现结构体排序关键语法要点(sort)
    理解 PHP 中的 Streams
    几款主流PHP框架的优缺点评比
    8个开发必备的PHP功能
    5个开发人员不应该错过的最好跨平台PHP编辑器
    推荐五款优秀的PHP代码重构工具
  • 原文地址:https://www.cnblogs.com/duan-to-success/p/3653928.html
Copyright © 2011-2022 走看看