先前是怎么都过不了,后来打了一张斐波那契表就过了,。<= 10^100的斐波那契数数有479个。
#include <stdio.h>
#include <string.h>
char res[150],res_1[150],res_2[150];
char all_res[600][130];
int all_res_len[600];
int res_top = 0,res_fir_top,res_sec_top;
int a_len,b_len;
int all_res_top = 0;
void add(){
res_fir_top = strlen(res_1);
res_sec_top = strlen(res_2);
res_top = 0;
while(res_fir_top && res_sec_top){
res[res_top++] = res_1[--res_fir_top] + res_2[--res_sec_top] - '0';
}
while(res_fir_top){
res[res_top++] = res_1[--res_fir_top];
}
while(res_sec_top){
res[res_top++] = res_2[--res_sec_top];
}
for(int i = 0; i < res_top; ++ i){
if(res[i] > '9'){
res[i] -= 10;
res[i+1] += 1;
}
}
if(res[res_top]){
res[res_top] += '0';
++res_top;
}
strcpy(res_2,res_1);
for(int i = res_top - 1;~i;--i,++res_fir_top){
res_1[res_fir_top] = res[i];
}
res_1[res_fir_top] = '\0';
strcpy(res,res_1);
return ;
}
void bulid_fi(){
memset(res,0,sizeof(res));
res_top = 0;
strcpy(res_1,"1");
strcpy(res_2,"0");
while(res_top < 105){
add();
strcpy(all_res[all_res_top++],res);
}
return;
}
int main(int argc, char *argv[])
{
int fi_num;
char a[150],b[150];
int cmp_a ,cmp_b;
// 建立斐波那契表;
bulid_fi();
for(int i = 0;i < all_res_top;++i){
all_res_len[i] = strlen(all_res[i]);
}
while(scanf("%s%s",a,b),strcmp(a,"0")||strcmp(b,"0")){
a_len = strlen(a);
b_len = strlen(b);
fi_num = 0;
for(int i = 0;i < all_res_top; ++i){
if(all_res_len[i] < a_len){
continue;
}else if(all_res_len[i] > a_len && all_res_len[i] < b_len){
++fi_num;
}else if(all_res_len[i] > b_len){
break;
}else{
cmp_a = strcmp(all_res[i],a);
cmp_b = strcmp(all_res[i],b);
if(all_res_len[i] == a_len&&all_res_len[i]==b_len){
if(cmp_a >= 0 && (cmp_b <= 0)){
++fi_num;
}
}else{
if(((all_res_len[i] == a_len)&&(cmp_a >= 0))||((all_res_len[i]==b_len)&&(cmp_b <= 0))){
++fi_num;
}
}
}
}
printf("%d\n",fi_num);
}
return 0;
}