HDU1316 fib+高精度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<stdlib.h> 5 using namespace std; 6 const int maxn = 205; 7 char a[ maxn ],b[ maxn ],s[ maxn ]; 8 char fi[ 505 ][ maxn ]; 9 char tmp[ maxn ]; 10 11 void a_b_add( ){ 12 int i,t,la,lb; 13 i=0; 14 t=0; 15 la=strlen(a)-1; 16 lb=strlen(b)-1; 17 while((la>=0)&&(lb>=0)) 18 { 19 s[i]=a[la--]+b[lb--]+t-48; 20 t=0; 21 if(s[i]>'9') 22 { 23 t=1; 24 s[i]=s[i]-10; 25 } 26 i++; 27 } 28 while((la>=0)&&(lb<0)) 29 { 30 s[i]=a[la--]+t; 31 t=0; 32 if(s[i]>'9') 33 { 34 s[i]=s[i]-10; 35 t=1; 36 } 37 i++; 38 } 39 while((la<0)&&(lb>=0)) 40 { 41 s[i]=b[lb--]+t; 42 t=0; 43 if(s[i]>'9') 44 { 45 s[i]=s[i]-10; 46 t=1; 47 } 48 i++; 49 } 50 s[i]='0'+t; 51 if(s[i]=='0') 52 i--; 53 int j=0; 54 for( ;i>=0;i-- ) tmp[ j++ ]=s[ i ]; 55 tmp[ j++ ]='\0'; 56 strcpy( s,tmp ); 57 return ; 58 }//a+b 高精度 59 60 int cmp( char s1[],char s2[] ){ 61 int len1,len2; 62 len1=strlen( s1 ),len2=strlen( s2 ); 63 if( len1>len2 ) return 1;// > 64 if( len1<len2 ) return -1;// < 65 if( strcmp( s1,s2 )==0 ) return 0;// = 66 if( strcmp( s1,s2)<0 ) return -1; 67 return 1; 68 } 69 70 int main(){ 71 a[ 0 ]=='1',a[ 1 ]='\0'; 72 b[ 0 ]='1',b[ 1 ]='\0'; 73 int cnt; 74 fi[ 0 ][ 0 ]='1',fi[ 1 ][ 0 ]='1'; 75 cnt=1; 76 for( int i=1;i<=500;i++ ){ 77 a_b_add( ); 78 strcpy( fi[ cnt ],s ); 79 strcpy( a,fi[ cnt ]),strcpy( b,fi[ cnt-1 ]); 80 //printf("%d %s\n",strlen(fi[cnt]),fi[ cnt ]); 81 cnt++; 82 } 83 84 while( scanf("%s %s",a,b)!=EOF ){ 85 if( a[0]=='0'&&b[0]=='0' ) break; 86 int left,right; 87 int temp; 88 for( int i=1;i<cnt;i++ ){ 89 temp=cmp( fi[ i ],a ); 90 if( temp>=0 ) {//fi[]>=a 91 left=i; 92 break; 93 } 94 } 95 for( int i=left;i<cnt;i++ ){ 96 temp=cmp( fi[ i ],b ); 97 if( temp>0 ) {//fi[]>a 98 right=i-1; 99 break; 100 } 101 else if( temp==0 ){ 102 right=i; 103 break; 104 } 105 } 106 //printf("left:%d right:%d\n",left,right); 107 printf("%d\n",right-left+1); 108 } 109 return 0; 110 }
keep moving...