• How many Fibs?(高精度斐波那契数) HDU1316


    Problem Description

    Recall the definition of the Fibonacci numbers:
    f1 := 1
    f2 := 2
    fn := fn-1 + fn-2 (n >= 3)

    Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].

    Input
    The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a <= b <= 10^100. The numbers a and b are given with no superfluous leading zeros.

    Output
    For each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b.

    Sample Input

    10 100
    1234567890 9876543210
    0 0

    Sample Output

    5
    4

    思路:好多人用java的BigInteger来做貌似简单,不过思路我觉的太麻烦,斐波那契数玩的不6,就老老实实的高精度比较吧。总的来说就是先把斐波那契数跑出来,自己测试一下会发现600以内足够了,然后每次for循环一遍计数输出就行了。

    代码:

    #include <iostream>
    #include <stdio.h>
    #include <string>
    
    using namespace std;
    
    string Add(string a,string b)
    {
        string ans;
        int lena = a.length();
        int lenb = b.length();
        if(lena > lenb)
        {
            for(int i=0 ; i<lena-lenb ; i++)
            {
                b = "0" + b;//注意0加在前面 
            }
        }
        else
        {
            for(int i=0 ; i<lenb-lena ; i++)
            {
                a = "0" + a;
            }
        }
        int len = a.length();
        int cf = 0;//进位
        for(int i=len-1 ; i>=0 ; i--)
        {
            int mid = a[i]-'0' + b[i]-'0' + cf;
            cf = 0;//注意进位清零 
            if(mid>9)
            {
                cf = 1;
                mid %= 10;
            }
            ans = char('0'+mid) + ans;
        }
        if(cf)ans = "1" + ans;//注意最后可能有进位 
        return ans;
    }
    
    int Compare(string a,string b)
    {
        if(a == b)return 0;
        int lena = a.length();
        int lenb = b.length();
        if(lena > lenb)return 1;
        else if(lena < lenb)return -1;
        else if(lena == lenb)
        {
            for(int i=0 ; i<lena ; i++)
            {
                if(a[i] > b[i])return 1;
                else if(a[i] < b[i])return -1;
            }
        }
    }
    
    string Fibo[600];
    
    int main()
    {
        Fibo[0] = "1";
        Fibo[1] = "2";
        for(int i=2 ; i<600 ; i++)
        {
            Fibo[i] = Add(Fibo[i-1],Fibo[i-2]);
        }
        string a,b;
        while(cin>>a>>b)
        {
            if(a == "0" && b == "0")break;
            if(b == "0")
            {
                printf("0
    ");
                continue;
            }
            int sum = 0;
            for(int i=0 ; i<600 ; i++)
            {
                if(Compare(a,Fibo[i]) == 1)continue;
                else if(Compare(b,Fibo[i]) == -1)break;
                sum++;
            }
            printf("%d
    ",sum);
        }
    
        return 0;
    }
  • 相关阅读:
    Tomcat内存溢出的三种情况及解决办法分析
    详解:贝叶斯算法(bayesian)在GCMail反垃圾邮件系统中的应用
    关于solr schema.xml 和solrconfig.xml的解释
    Java流操作,InputStream、OutputStream及子类FileInputStream、FileOutputStream;BufferedInpu
    Introduce Null Object(引入Null对象)
    Solr Schema.xml和solrconfig.xml分析
    @InitBinder
    程序员修炼之道
    JSTL 标签库详细介绍资料
    Spring Mvc入门实例
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514173.html
Copyright © 2020-2023  润新知