How many Fibs?(poj 2413)大数斐波那契

http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/C

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int a[601][1000];
char str[601][1001];
int main()
{
    char m[601],n[601];
    int i,j,sum;
    memset(a,0,sizeof(a));//大数斐波那契,主要是了解思想
    a[1][0]=1;
    a[2][0]=2;
    a[3][0]=3;
    for (i=4;i<=600;i++)
    {
        for (j=0;j<=501;j++)
        {
            a[i][j]=a[i][j]+a[i-1][j]+a[i-2][j];
            if (a[i][j]>9)
            {
                a[i][j+1]=a[i][j]/10;
                a[i][j]=a[i][j]%10;
            }
        }
    }
    int flag=0,k;
    for(int i=1;i<=600;i++)
    {
        flag=0;
        k=0;
        for(int j=500;j>=0;j--)
        {
            if(flag||a[i][j])
            {
                flag=1;
                str[i][k]=a[i][j]+'0';
                k++;
            }
        }
        str[i][k]='\0';
    }
    flag=0;
    /*for(i=100;i>=0;i--)
    {
        if(flag||a[100][i])
        {
            flag=1;
            printf("%d",a[100][i]);
        }
    }*/
    /*for(int i=40;i<=50;i++)
    printf("%s\n",str[i]);*/
    int l1,l2;
    while(scanf("%s%s",n,m)!=EOF)
    {
        sum=0;
         l1=strlen(n);
         l2=strlen(m);
        if(n[0]=='0'&&m[0]=='0') break;
        for(int i=1;i<=500;i++)
        {
            if((strlen(str[i])>l1&&strlen(str[i])<l2))//如果这个数的长度在范围之(a,b)长度之间,则这个数一定属于(a,b);
            {
                 sum++;
            }
            else if(l1==l2&&strlen(str[i])==l1&&strcmp(str[i],n)>=0&&strlen(str[i])==l2&&strcmp(str[i],m)<=0)//如果(a,b)两个数长度一样,则比较他们在字典中的大小。
            {
                  sum++;
            }
            else if(l1!=l2&&strlen(str[i])==l1&&strcmp(str[i],n)>=0)
            {
                sum++;
            }
            else if(l1!=l2&&strlen(str[i])==l2&&strcmp(str[i],m)<=0)
            {

                sum++;
            }
        }
        printf("%d\n",sum);;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值