//  大数继续

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
 
Source
 

/**********************

高精度斐波数,依然用高精度的加法模板打表,打到520多就够了,

主要是判断数的大小,这里是比较额字符串形式的数的大小。

***********************/

Code:

#include<iostream>
#include <stdio.h>
#include<string>
using namespace std;
string add(string x,string y)
{
string ans ;
int lenx = x.length();
int leny = y.length();
if(lenx<leny)
{
for(int i = 1;i<=leny-lenx;i++)
x = "0"+x;
}
else
{
for(int i = 1;i<=lenx-leny;i++)
y = "0"+y;
}
lenx = x.length();
int cf = 0;
int temp;
for(int i = lenx-1;i>=0;i--)
{
temp = x[i] - '0' + y[i] - '0'+cf;
cf = temp/10;
temp%=10;
ans = char('0'+temp)+ans;
}
if(cf!=0)
ans = char(cf+'0')+ans;
return ans;
}
int compare(string x,string y)// 字符串形式的数的比较大小
{
int i,lenx = x.length(),leny = y.length(),leaf;
if(x==y) return 0;// 0 表示 x == y
if(x.length()>y.length()) return 1;// 返回1 表示 x > y
if(x.length()<y.length()) return -1;// -1 表示 x < y
if(x.length()==y.length())
{
for(i = 0;i<lenx;i++)
{
if(x[i]==y[i]) continue;
if(x[i]>y[i]) return 1;
else return -1;
}
return 0;
}
return leaf;
}
int main()
{
int i,j,k,start,eend;
string x,y,num[1005];;
num[0] = "0";
num[1] = "1";
num[2] = "2";
for(int i = 3;i<=1000;i++)
num[i] = add(num[i-1],num[i-2]);
while(cin>>x>>y&&x!="0"||y!="0")// x y 均为 0 的时候才结束程序
{
if(y == "0")// y == 0 时 直接输出 0
{
printf("0");
continue;
}
start = eend = 0;
/**
j = k = 0;
while(x[j]=='0')// 受到
j++;
x = x.substr(j,x.length()-j);// 受到 hdu 1753 的影响,以为会有前导0,其实没有 while(y[k]=='0')
k++;
y = y.substr(k,y.length()-k);
**/
for(i = 1;i<1000;i++)
{
if(compare(x,num[i])==0)
{
start = i;
break;
}
else if(compare(num[i],x)==-1&&compare(num[i+1],x)==1)
{
start = i+1;
break;
}
}
for(i = 1;i<1000;i++)
{
if(compare(y,num[i])==0){
eend = i;break;
}
else if(compare(num[i],y)==-1&&compare(num[i+1],y)==1){
eend = i;break;
}
}
if(x=="0") // 注意 x == 0 时的情况
start = 1;
//cout<<start<<" "<<eend<<endl;;
cout<<eend-start+1<<endl;
}
}

hdu 1316 How many Fibs?(高精度斐波那契数)的更多相关文章

  1. HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...

  2. hdu 4983 线段树+斐波那契数

    http://acm.hdu.edu.cn/showproblem.php?pid=4893 三种操作: 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 从l到r区间 ...

  3. HDU.2516 取石子游戏 (博弈论 斐波那契博弈)

    HDU.2516 取石子游戏 (博弈论 斐波那契博弈) 题意分析 简单的斐波那契博弈 博弈论快速入门 代码总览 #include <bits/stdc++.h> #define nmax ...

  4. HDU 2516 取石子游戏(斐波那契博弈)

    取石子游戏 Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  5. HDU 5620 KK's Steel (斐波那契序列)

    KK's Steel 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/J Description Our lovely KK ha ...

  6. hdu 2516 取石子游戏 (斐波那契博弈)

    题意:1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍. 取完者胜,先取者负输出"Second win",先取者胜 ...

  7. HDU 1021(斐波那契数与因子3 **)

    题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...

  8. hdu1316(大数的斐波那契数)

    题目信息:求两个大数之间的斐波那契数的个数(C++/JAVA) pid=1316">http://acm.hdu.edu.cn/showproblem.php? pid=1316 这里 ...

  9. hdu1568&&hdu3117 求斐波那契数前四位和后四位

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...

随机推荐

  1. matlab s变换

    A4=readdata('E:\mydata.TXT');[st,t,f] = st(A4(1:1000,2)); surf(t,f,10*log10(abs(st)),'EdgeColor','no ...

  2. Android设计模式系列--原型模式

    CV一族,应该很容易理解原型模式的原理,复制,粘贴完后看具体情况是否修改,其实这就是原型模式.从java的角度看,一般使用原型模式有个明显的特点,就是实现cloneable的clone()方法.原型模 ...

  3. postfix反垃圾邮件说明

    参考地址:http://guailele.blog.51cto.com/1156442/780223 1.打开 smtp 的认证模块 在/etc/postfix/main.cf文件最后加上:   sm ...

  4. 模拟京东商城登陆HttpRequest

    利用Winform HttpRequest 模拟登陆京东商城 目前只获取订单信息,可以获取图片等其他信息 using System; using System.Collections.Generic; ...

  5. Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS

    D. Phillip and Trains Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/ ...

  6. 【C/C++多线程编程之六】pthread相互排斥量

    多线程编程之线程同步相互排斥量       Pthread是 POSIX threads 的简称,是POSIX的线程标准.          Pthread线程同步指多个线程协调地,有序地同步使用共享 ...

  7. Windows二进制文件的Python扩展包

    http://www.lfd.uci.edu/~gohlke/pythonlibs/ https://pypi.python.org/simple/

  8. 【转】IT职场人生系列之四:怎样写简历

    本文是IT职场人生系列的第四篇. 因为早年跳槽无数,所以积累了不少"技巧",逐渐变成写简历的"专家",最长的时候简历到了12页,所以现在练就一手写长篇博客的功夫 ...

  9. 一个小例子讲讲jsonp

    1.何为jsonp(json with padding) json我们都知道并用过.那么jsonp呢,呃,好像听过,但没用过.很久以来楼主也只是听过这个名词而已.直到今晚楼主看到一篇文章(http:/ ...

  10. 使用socket实现FTP程序

    #-*- coding:utf-8 -*- import socketserver from module import * class server: def __init__(self,reque ...