uva 10183 How many Fibs?



Problem B: How many Fibs?

Recall the definition of the Fibonacci numbers:

f 1  := 1  
f 2  := 2  
f n  :=   f n-1  +   f n-2      (n>=3)
Given two numbers  a  and  b , calculate how many Fibonacci numbers are in the range [  ab ].

Input Specification

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<=10100. The numbers a and b are given with no superfluous leading zeros.

Output Specification

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
题意:每个数都是由前两的和所求;输入两个数a,b询问这两个之间包含a,b之间有多少个数,由于数字较大必须字符串处理,要用到大数加法,在查找时要用到字符串的比较
 
 
 
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
string ss[2000];
string add(string a,string b)//  大数加法 
{
    string s;
    int i=0,j=0,l=0;
    int m,ans=0,k;
    while(a[i]&&b[j])
    {
        m=a[i]-'0'+b[j]+ans-'0';
        ans = m / 10;
        s+=(m%10+'0');
        i++;
        j++;
    }
    if(i==a.size())
    {
        while(i != b.size())
        {
            m = ans + b[i] - '0';
            ans = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(ans) s += ans + '0';
    }
    else if(i==b.size())
    {
        while(i != a.size())
        {
            m = ans + a[i] - '0';
            ans = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(ans) s += ans + '0';
    }
    reverse(s.begin(), s.end());
    return s;
}
int compare(string a,string b)// 字符串比较函数
{
    if(a.size()!=b.size())
    {
        if(a.size()>b.size())
            return 1;
        if(a.size()<b.size())
            return -1;
    }
    else if(a.size()==b.size())
    {
        for(int i=0; i<a.size(); i++)
        {
            if(a[i]>b[i])
                return 1;
            else if(a[i]<b[i])
                return -1;

        }
        return 0;
    }
}
int main()
{
    int i;
    ss[1]="1";
    ss[2]="2";
    for(i=3; i<1500; i++)
    {
        reverse(ss[i-1].begin(),ss[i-1].end());
        reverse (ss[i-2].begin(),ss[i-2].end());
        ss[i]=add(ss[i-1],ss[i-2]);
        reverse(ss[i-1].begin(),ss[i-1].end());
        reverse(ss[i-2].begin(),ss[i-2].end());
    }
//    for(i=30;i<=40;i++)
//       cout<<ss[i]<<endl;
    string a,b;
    while(cin>>a>>b&&a[0]!='0'||b[0]!='0')//在不等于零时要用或不能用且要不输入0 1 是无结果
    {
        int ans=0;
        for(i=1; i<1500; i++)
        {
            if((compare(ss[i],a)>=0&&compare(ss[i],b)<=0))
            {
                ans++;
            }
           else if(compare(ss[i],b)>0)
                break;
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值