hdu 1316 poj 2413 how many Fibs


先前是怎么都过不了,后来打了一张斐波那契表就过了,大哭。<= 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;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值