2018_2_2_How Many Fibs? _打表

13 篇文章 0 订阅
2 篇文章 0 订阅
How many Fibs?
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12413 Accepted: 4431

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

Source

Ulm Local 2000
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<fstream>

const int N=1000;

using namespace std;
ofstream out;
struct bignumber{
	int len;
	int c[N];
	void setnum(char s[]){
		len=0;
		memset(c,0,sizeof(c));
		for(int i=strlen(s)-1;i>=0;i--){
			c[len++]=s[i]-'0';
		}
	}
	bool iszero() {
		return len==1&&c[0]==0;
	}
}big[1000];

void add(bignumber &a,bignumber &b,bignumber &c){
	int i, carry=0;
	c.len=a.len+b.len-1;
    for (i=0;i<c.len;i++){
    	c.c[i] = carry + a.c[i] + b.c[i];
    	carry = c.c[i] / 10;
    	c.c[i] %= 10;
  	}
  	if(carry){
  		c.c[c.len++]=carry;
	}else{
		while(!c.c[c.len]){
			c.len--;
		}c.len++;
	}
}

char s[N],t[N];
bignumber a,b;

bool cmp(bignumber n,bignumber m){
	if(n.len==m.len){
		int len=n.len;
		while(len>=0){
			if(n.c[len]!=m.c[len])
			return n.c[len]<m.c[len];
			len--;
		}
		return true;
	}
	return n.len<m.len;
}

int main(){
	big[0].setnum("1");
	big[1].setnum("2");
	//out<<'"'<<'1'<<'"'<<',';
	for(int i=2;i<500;i++){
		add(big[i-2],big[i-1],big[i]);
	}
	int n,cnt1,cnt2;
	while(scanf("%s%s",s,t)!=EOF){
		a.setnum(s);
		b.setnum(t);
		if(a.iszero()&&b.iszero())return 0;
		int cnt=0;
		for(int i=0;i<N;){
			bool f1=cmp(a,big[i]);
			bool f2=cmp(big[i],b);
			if(!f1){
				i++; 
			}else if(!f2){
				break;
			}else{
				cnt++;
				i++; 
			}
		}
		printf("%d\n",cnt);
	}
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值