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;
}

 

posted @ 2014-08-20 15:11  人艰不拆_zmc  阅读(333)  评论(0编辑  收藏  举报