UVA - 10183 How Many Fibs? —— 大数斐波那契

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值