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
题目意思是给你两个数,让你输出两个数(闭区间)之间的菲波那切数有多少个,当输入0 0时结束。
思路:这是一道典型的打表查询题,使用大数相加算出1-10^100之内的菲波那切数存起来就行了,然后查询
菲波那切数列:f[n]=f[n-1]+f[n-2]
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
char f[1100][550];
int f1[550],f2[550];
char a[550],b[550];
void feibo()
{
strcpy(f[0],"1");
strcpy(f[1],"2");
int i,j,k;
for(i=2;i<1000;i++)
{
int l1=strlen(f[i-2]);
int l2=strlen(f[i-1]);
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(j=l1-1;j>=0;j--)
f1[j]=f[i-2][l1-j-1]-'0';
for(j=l2-1;j>=0;j--)
f2[j]=f[i-1][l2-j-1]-'0';
int l;
int jinwei=0;
if(l1>l2)
l=l1;
else
l=l2;
while(j<=l)
{
f1[j]=f1[j]+f2[j]+jinwei;
if(f1[j]>9)
{
f1[j]-=10;
jinwei=1;
}
else
jinwei=0;
j++;
}
while(f1[l]==0)
l--;
for(j=l;j>=0;j--)
f[i][j]=f1[l-j]+'0';
}
return;
}
int main()
{
feibo();
while(scanf("%s",a)&&scanf("%s",b))
{
if(a[0]=='0'&&b[0]=='0')
break;
int l1,l2,l;
l1=strlen(a);
l2=strlen(b);
int sum=0;
for(int i=0;i<1000;i++)
{
l=strlen(f[i]);
if(l1==l2)
{
if(l==l1)
if((strcmp(f[i],a)==0||strcmp(f[i],a)>0)&&(strcmp(f[i],b)<0||strcmp(f[i],b)==0))
sum++;
}
if(l1!=l2)
{
if(l1==l)
if(strcmp(f[i],a)==0||strcmp(f[i],a)>0)
sum++;
if(l1<l&&l<l2)
sum++;
if(l==l2)
if(strcmp(f[i],b)<0||strcmp(f[i],b)==0)
sum++;
}
if(l>l2)
break;
}
printf("%d\n",sum);
}
return 0;
}
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
题目意思是给你两个数,让你输出两个数(闭区间)之间的菲波那切数有多少个,当输入0 0时结束。
思路:这是一道典型的打表查询题,使用大数相加算出1-10^100之内的菲波那切数存起来就行了,然后查询
菲波那切数列:f[n]=f[n-1]+f[n-2]
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
char f[1100][550];
int f1[550],f2[550];
char a[550],b[550];
void feibo()
{
strcpy(f[0],"1");
strcpy(f[1],"2");
int i,j,k;
for(i=2;i<1000;i++)
{
int l1=strlen(f[i-2]);
int l2=strlen(f[i-1]);
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(j=l1-1;j>=0;j--)
f1[j]=f[i-2][l1-j-1]-'0';
for(j=l2-1;j>=0;j--)
f2[j]=f[i-1][l2-j-1]-'0';
int l;
int jinwei=0;
if(l1>l2)
l=l1;
else
l=l2;
while(j<=l)
{
f1[j]=f1[j]+f2[j]+jinwei;
if(f1[j]>9)
{
f1[j]-=10;
jinwei=1;
}
else
jinwei=0;
j++;
}
while(f1[l]==0)
l--;
for(j=l;j>=0;j--)
f[i][j]=f1[l-j]+'0';
}
return;
}
int main()
{
feibo();
while(scanf("%s",a)&&scanf("%s",b))
{
if(a[0]=='0'&&b[0]=='0')
break;
int l1,l2,l;
l1=strlen(a);
l2=strlen(b);
int sum=0;
for(int i=0;i<1000;i++)
{
l=strlen(f[i]);
if(l1==l2)
{
if(l==l1)
if((strcmp(f[i],a)==0||strcmp(f[i],a)>0)&&(strcmp(f[i],b)<0||strcmp(f[i],b)==0))
sum++;
}
if(l1!=l2)
{
if(l1==l)
if(strcmp(f[i],a)==0||strcmp(f[i],a)>0)
sum++;
if(l1<l&&l<l2)
sum++;
if(l==l2)
if(strcmp(f[i],b)<0||strcmp(f[i],b)==0)
sum++;
}
if(l>l2)
break;
}
printf("%d\n",sum);
}
return 0;
}