UVa 10183 - How Many Fibs?

来源:互联网 发布:java 中checkRadio用法 编辑:程序博客网 时间:2024/05/16 15:06

题目:统计区间中的FIb个数。

分析:模拟、大整数。模拟大整数运算,统计即可。

注意:边界数据。

#include <stdio.h>#include <stdlib.h>#include <string.h>int  F[515][105];int  a[105];int  b[105];char C[105];char A[105];char B[105];int cmp( int* s, int* t ){for ( int i = 101 ; i >= 0 ; -- i )if ( s[i] != t[i] ) return s[i]-t[i];return 0;}int main(){//打表 F[0][0] = F[1][0] = 1;for ( int i = 2 ; i <= 510 ; ++ i ) {for ( int j = 0 ; j <= 101 ; ++ j )F[i][j] = F[i-1][j]+F[i-2][j];for ( int j = 0 ; j <= 101 ; ++ j ) {F[i][j+1] += F[i][j]/10;F[i][j+0] %= 10;}}while ( scanf("%s%s",A,B) != EOF ) {if  ( !strcmp(B,"0") ) break;memset( a, 0, sizeof(a) );memset( b, 0, sizeof(b) );int l1 = strlen(A);for ( int i = 0 ; i < l1 ; ++ i )a[l1-1-i] = A[i]-'0';int l2 = strlen(B);for ( int i = 0 ; i < l2 ; ++ i )b[l2-1-i] = B[i]-'0';int count1 = 0;for ( int i = 1 ; i <= 500 ; ++ i )if ( cmp( F[i], a ) < 0 )count1 ++;else break;int count2 = 0;for ( int i = 1 ; i <= 500 ; ++ i )if ( cmp( F[i], b ) <= 0 )count2 ++;else break;printf("%d\n",count2-count1);}return 0;}


原创粉丝点击