高精度问题请问各位,java实现更好

neupioneer 2005-05-25 10:49:11
How many Fibs?

Time Limit:1000MS Memory Limit:65536K
Total Submit:618 Accepted:176

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<=10100. 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
...全文
58 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nasi00 2005-05-25
  • 打赏
  • 举报
回复
楼主怎么在c/c++版问java阿,给你个c++的求fib的程序看看吧:

#include <stdio.h>

#include <iostream.h>

#include <string.h>

#define MAX 500

class number {
int digit[MAX];
public:
number();
number(char *);
void add(number &);
void print();
void set(char *);
};

number::number()
{
for(int i=0;i<MAX;i++)
digit[i]=0;
}

number::number(char *s)
{
int l=strlen(s),i;
for(i=0;i<l;i++)
digit[i]=(int)(s[l-i-1]-'0');
for(i=l;i<MAX;i++)
digit[i]=0;
}

void number::add(number &t)
{
int carrier=0;
for(int i=0;i<MAX;i++){
digit[i]=digit[i]+t.digit[i]+carrier;
carrier=digit[i]/10;
digit[i]%=10;
}
}

void number::print()
{
int i=MAX-1,j; bool flag=true;
while(!digit[i])
i--;
for(j=i;j>=0;j--) {cout<<digit[j];flag=false;}
if(flag) cout<<'0';
}

void number::set(char *s)
{
int i,l=strlen(s);
for(i=0;i<l;i++)
digit[i]=(int)(s[l-i-1]-'0');
for(i=l;i<MAX;i++)
digit[i]=0;
}
number f[500],t;
int main()
{
f[0].set("0");
f[1].set("1");
f[2].set("1");
int i;

for(i=3;i<=500;i++) {
t=f[i-1];
t.add(f[i-2]);
f[i]=t;
}

int n;
while(cin>>n) {
cout << "The Fibonacci number for "<<n<<" is ";
f[n].print();
cout<<endl;
}

return 0;
}
sunman1982 2005-05-25
  • 打赏
  • 举报
回复
没有好不好只有更合适的

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧