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
题意:问a,b之间的斐波那契数的个数,ab最大有100位
思路:大数,我用的vector来模拟,写的有些麻烦了
这篇用数组写的很简洁很不错http://blog.csdn.net/u013451221/article/details/29792437
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define max_ 50
using namespace std;
vector<int>v[1000];
void add(int a,int b,int c)
{
int l=min(v[a].size(),v[b].size());
int f=0,i;
for(i=0;i<l;i++)
{
int d=v[a][i]+v[b][i]+f;
if(d>9)
{
f=d/10;
d=d%10;
}
else
f=0;
v[c].push_back(d);
}
if(v[a].size()<v[b].size())
{
for(;i<v[b].size();i++)
{
int d=v[b][i]+f;
if(d>9)
{
f=d%10;
d=d/10;
}
else
f=0;
v[c].push_back(d);
}
}
else if(f)
v[c].push_back(f);
}
int cmp(int x,string s)
{
if(v[x].size()<s.size())
return -1;
if(v[x].size()>s.size())
return 1;
int l=v[x].size();
for(int i=l-1;i>=0;i--)
{
// printf("%d %d\n",v[x][i],s[l-i]-'0' );
if(v[x][i]<s[l-i-1]-'0')
return -1;
else if(v[x][i]>s[l-i-1]-'0')
return 1;
}
return 0;
}
int main(int argc, char const *argv[]) {
v[1].push_back(1);
v[2].push_back(2);
for(int i=3;i<1000;i++)
{
add(i-2,i-1,i);
}
string l,r;
while(cin>>l>>r)
{
if(l=="0"&&r=="0")
break;
int cnt=0,i=1;
while(cmp(i,l)==-1)
i++;
while(cmp(i,r)!=1)
{
i++;
cnt++;
}
printf("%d\n",cnt );
}
return 0;
}