HDU 1316 (斐波那契数列,大数相加,大数比较大小)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1316

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
 
代码如下:
#include<bits/stdc++.h>
using namespace std;
string str[500];
string add(string str1,string str2)//大数相加
{
    int l1=str1.length();
    int l2=str2.length();
    if(l1>l2)
    {
        for(int i=0;i<l1-l2;i++)
        {
            str2="0"+str2;
        }
    }else if(l1<l2)
    {
        for(int i=0;i<l2-l1;i++)
        {
            str1="0"+str1;
        }
    }
    l1=str1.length();
    string str3;
    int c=0;
    for(int i=l1-1;i>=0;i--)
    {
        int temp=str1[i]-'0'+str2[i]-'0'+c;
        c=temp/10;
        temp=temp%10;
        str3=char(temp+'0')+str3;
    }
    if(c!=0)
    {
        str3=char(c+'0')+str3;
    }
    return str3;
}
int compare(string str1,string str2)//str1大于等于str2,返回1
{
    int l1=str1.length();
    int l2=str2.length();
    if(l1>l2)
    {
        return 1;
    }else if(l1<l2)
    {
        return 0;
    }else
    {
        for(int i=0;i<l1;i++)
        {
            if(str1[i]>str2[i])
            {
                return 1;
            }else if(str1[i]==str2[i])
            {
                continue;
            }else
            {
                return 0;
            }
        }
    }
    return 1;
}
int f(string str1,string str2)//找出两个大数中间的斐波那契数的个数,包括边界
{
    int l1=str1.length(),l2=str2.length(),index1,index2;
    for(int i=0;i<=500;i++)
    {
        int k=str[i].length();
        if(k<l1)
            continue;
        else
        {
            index1=i;
            break;
        }
    }
    for(int i=499;i>=0;i--)
    {
        int k=str[i].length();
        if(k>l2)
            continue;
        else
        {
            index2=i;
            break;
        }
    }
    int r=0;
    for(int i=index1;i<=index2;i++)
    {
        if(compare(str[i],str1)==1&&compare(str2,str[i])==1)
        {
            r++;
        }
    }
    return r;
}
int main()
{
    string str1,str2;
    str[0]="1";
    str[1]="2";
    for(int i=2;i<500;i++)//先求出需要的斐波那契数列,第500个斐波那契数大于10的1000次方
    {
        str[i]=add(str[i-1],str[i-2]);
    }
    while(cin>>str1>>str2)
    {
        if(str1=="0"&&str2=="0")
            break;
        int r=f(str1,str2);
        printf("%d\n",r);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值