How Many Fibs? hdu1316
2011-05-27 21:15
260 查看
//大整数的加法,花了我两个小时,一定要小心仔细才行呀; //笨(本)人代码如下 #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;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 [i]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
相关文章推荐
- hdu1316 How Many Fibs? (java大数练习第二弹)
- hdu1316(How Many Fibs?)高精度
- hdu1316 How Many Fibs?
- HDU1316 How Many Fibs? 【大数】
- HDU1316:How Many Fibs?(大数JAVA)
- HDU1316:How Many Fibs?
- hdu1316 How Many Fibs?(大数)
- Java 大数之大数高精度斐波那契 UVA_10579&&HDU1316 How many Fibs?
- How Many Fibs? hdu1316 高精度
- hdu1316 How Many Fibs?
- How many Fibs?(poj 2413)大数斐波那契
- How many Fibs?(java大数)
- UVA10183 - How Many Fibs?(java大数+二分)
- HDUOJ - 1316 How Many Fibs?
- POJ 2413 How many Fibs?(高精度)
- How many fibs
- hdu 1316 How Many Fibs?
- hdu 1316 How many Fibs?(高精度斐波那契数)
- poj 2413 java How many Fibs?
- How many Fibs?(java)