How Many Fibs? hdu1316

 

 
  
// 大整数的加法,花了我两个小时,一定要小心仔细才行呀;
// 笨(本)人代码如下
#include < stdio.h >
#include
< string .h >
int fib[ 510 ][ 120 ]; // the first element is the array length
int a[ 120 ],b[ 120 ];

void change( int * x) // right
{
int i,temp,len;
len
= x[ 0 ];
for (i = 1 ;i <= len / 2 ;i ++ )
{
temp
= x[i];x[i] = x[len + 1 - i];x[len + 1 - i] = temp;
}
}

void countfib() // right
{
int k,i,len1,len2,len,carry;
memset(fib,
0 , sizeof (fib));
fib[
1 ][ 0 ] = fib[ 2 ][ 0 ] = 1 ;
fib[
1 ][ 1 ] = 1 ;fib[ 2 ][ 1 ] = 2 ;
for (k = 3 ;k < 510 ;k ++ ) // compute the fib ,is the most important and most difficult of there
{ // 打表算出在其范围内所有的fibs数
len1 = fib[k - 2 ][ 0 ];len2 = fib[k - 1 ][ 0 ];
len
= len1 > len2 ? len1:len2;
carry
= 0 ;
change(fib[k
- 2 ]); // 算前将其反置,便于计算,第0个数保存其长度。
change(fib[k - 1 ]);
for (i = 1 ;i <= len + 1 ;i ++ )
{
fib[k][i]
= fib[k - 2 ][i] + fib[k - 1 ][i] + carry;
if (fib[k][i] >= 10 )
{
fib[k][i]
-= 10 ;
carry
= 1 ;
}
else
carry
= 0 ;
}
if (fib[k][len + 1 ] == 0 )
fib[k][
0 ] = len;
else
fib[k][
0 ] = len + 1 ;
change(fib[k]);
change(fib[k
- 1 ]); // 一定要记得算完后所有的数都要转换回来。
change(fib[k - 2 ]);
}

}

int compare( int * x, int * y) // x>y 0, x==y 2, x<y 1 right
{
int len1,len2,i;
len1
= x[ 0 ];len2 = y[ 0 ];
if (len1 > len2)
return 0 ;
else if (len1 < len2)
return 1 ;
else
{
for (i = 1 ;i <= len1;i ++ )
{
if (x[i] > y[i])
return 0 ;
else if (x[i] < y[i])
return 1 ;
}
return 2 ;
}
}

int main()
{
int i,j,flag,count;
int start,end;
char s1[ 120 ],s2[ 120 ];
countfib();
while ( 1 )
{
scanf(
" %s " ,s1);
scanf(
" %s " ,s2);
if (s1[ 0 ] == ' 0 ' && s2[ 0 ] == ' 0 ' )
break ;
memset(a,
0 , sizeof (a));
memset(b,
0 , sizeof (b));
for (i = 0 ;s1[i];i ++ )
a[i
+ 1 ] = s1[i] - ' 0 ' ;
a[
0 ] = i;
for (i = 0 ;s2[i];i ++ )
b[i
+ 1 ] = s2[i] - ' 0 ' ;
b[
0 ] = i;
count
= 0 ;

for (i = 1 ;;i ++ )
{
flag
= compare(a,fib[i]);
if (flag > 0 )
{ start
= i; break ;}
}
for (i = 1 ;;i ++ )
{
flag
= compare(b,fib[i]);
if (flag == 1 )
{ end
= i - 1 ; break ;}
else if (flag == 2 )
{ end
= i; break ;}
}
count
= end - start + 1 ;
printf(
" %d\n " ,count);
/* printf("start=%d end=%d count=%d\n",start,end,count);//有时打印中间值查看错误是一种很好的习惯。
for(i=start;i<=end;i++)
{
for(j=1;j<=fib[i][0];j++)
printf("%d",fib[i][j]);
printf("\n");
}
*/
}
return 0 ;
}

How Many Fibs?

Problem Description hdu1316 

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

转载于:https://www.cnblogs.com/tonyspace/archive/2011/05/27/2060278.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值