栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

ZCMU--1041: 二哥的困惑 Ⅳ(C语言)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ZCMU--1041: 二哥的困惑 Ⅳ(C语言)

Description

A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

Your task is to take a number as input, and print that Fibonacci number.

Input
a positive integer number N. Output

print the Nth Fibonacci number.

Sample Input 100 Sample Output 354224848179261915075 解析:让我们求第N个斐波那契数,高精度。我们运用高精度a+b的做法即可,开3个数组,a[ ],b[ ],c[ ],让c[]=a[]+b[],计算完之后,我们再将a[ ]->b[ ],c[ ]->a[ ],然后再将c[ ]清空,为下一轮a[ ]+b[ ]=c[ ],如此反复,实现f(n)=f(n-1)+f(n-2).然后计算到第N项,将答案数组c[ ]逆序输出即可。
#include 
int a[1005],b[1005],c[1005];//实现a[]+b[]=c[],c[]作为答案数组 
int main()
{
	int n,x,z,p,i,s;
	while(~scanf("%d",&n)){
		x=1,p=1,a[0]=1,b[0]=1;	//初始一下,x表示a[]的位数,p表示c[]的位数 
		if(n==1||n==2){		//众所周知,直接输出 
			printf("1n");
			continue;
		}
		for(i=3;i<=n;i++){
			for(s=0;s=10) c[z+1]+=c[z]/10; //c[]逐位判断进位 
				c[z]%=10;
			}
			while(c[p]>0){	//核心 
				p++;	//之前所有进位都堆到了,c[p]>0,就像55+55=110,位数肯定+1 
				while(c[p-1]>=10) c[p]+=c[p-1]/10,c[p-1]%=10,p++;
				//c[p]已经是当前最高位了,如果>=10,那么继续进位,c[]位数也就+1 
			}
			for(z=0;z=0;i--) printf("%d",c[i]),c[i]=0,a[i]=0,b[i]=0;//边输出边初始化
		printf("n");
	}
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号