How Many Fibs? (大数)

How Many Fibs?

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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

Source

University of Ulm Local Contest 2000




题意:就是给出a,b;(a<b)求出a,b之间的斐波那契数的个数;
思路:先求出1-500的斐波那契数(因为第500个斐波那契数就已经超过100位了)
代码:
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
char f[501][1000];
void add(char a[],char b[],char s[])//大数加法函数(斐波那契数用大数加法来做的)
{
    int i,j,k,up,x,y,z,l;
    char *c;
    if (strlen(a)>strlen(b)) l=strlen(a)+2;
    else l=strlen(b)+2;
    c=new char[l];
    i=strlen(a)-1;
    j=strlen(b)-1;
    k=0;
    up=0;
    while(i>=0||j>=0)
    {
        if(i<0) x='0';
        else x=a[i];
        if(j<0) y='0';
        else y=b[j];
        z=x-'0'+y-'0';
        if(up) z+=1;
        if(z>9)
        {
            up=1;
            z%=10;
        }
        else up=0;
        c[k++]=z+'0';
        i--;
        j--;
    }
    if(up) c[k++]='1';
    i=0;
    c[k]='\0';
    for(k-=1; k>=0; k--)
        s[i++]=c[k];
    s[i]='\0';
}
int cmp(char a[],char b[])//比较函数就是先看长度,再看字符(和strcmp函数差不多)相等时返回0,大于时,返回正数,小于时,返回负数。
{
    int i,len1,len2;
    len1=strlen(a);
    len2=strlen(b);
    if(len1>len2)
        return 1;
    else if(len1<len2)
        return -1;
    else if(len1==len2)
    {
        for(i=0;i<len1;i++)
        {
            if(a[i]!=b[i])
            {
                break;
            }
        }
        return a[i]-b[i];
    }
}
int main()
{
    int i,len1,len2,m,t,s;
    char a[102],b[102];
    f[0][0]='1';
    f[1][0]='1';
    for(i=2;i<501;i++)
    {
        add(f[i-1],f[i-2],f[i]);
    }
    while((scanf("%s%s",a,b)!=EOF)&&!(a[0]=='0'&&b[0]=='0'))
    {
        m=0;
        len1=strlen(a);
        len2=strlen(b);
        for(i=1;i<501;i++)
        {
            t=cmp(a,f[i]);
            s=cmp(b,f[i]);
            if((t<0||(t==0))&&(s>0||s==0))//判断斐波那契数是否在a,b之间,再,就计数;
            m++;
        }
        cout<<m<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值