[深入浅出学算法][递推专题] 暴力朴素的Fib

[深入浅出学算法][递推专题] 暴力朴素的Fib

题目描述

F(0)=F(1)=1;对于n>=2,F(n)=F(n-1)+F(n-2),请输出F(n)

 

输入

第一行输入一个整数n(1<=n<=50)

 

输出

F(n)

 

样例输入

10

 

样例输出

89

题解:

        本题其实就是题目花俏一点儿了,其余的都还好——一道普通的费波纳茨题。本题我提供两种方法,第一种(不用函数的递推):先把f(0)和f(1)赋值为1,由于“n>=2,F(n)=F(n-1)+F(n-2)”这句话,网络上一些的题解和代码都会答案错误11%,然后判断n是否小于2,然后就不说了(往下翻看第一个代码),总体思想——递推,递推,再递推;第二种:这种方法适合零基础L1的同学,把n的50种可能(1~50)全部列出来(不过,我是“作弊的”——用第一种方法编译运行[第一种我弄成了多组],然后把一个一个输出结果复制过去),希望看过我博客的人千万不要用第二种方法,否则可能会降低自己的“身价”(除非真的不会),第二种方法,我即使是复制的也搞了10多分钟。废话不说,让我们来动最精彩的环节——“源代码环节”。

源代码:

第一种方法源代码(递推):

#include<bits/stdc++.h>
using namespace std;
int main() {
	long long f0,f1,f;
	int i,n;
	while(scanf("%d",&n)!=EOF) {
		f0=f1=1;
		if(n<2) f=1;
		else {
			for(i=2; i<=n; i++) {
				f=f0+f1;
				f0=f1;
				f1=f;
			}
		}
		printf("%lld\n",f);
	}
	return 0;
}

上面的代码,各位也可以不用多组。(我只是方便我复制而已)

第二种方法源代码(暴力枚举):

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	if(n==1) cout<<1<<endl;
	else if(n==2) cout<<2<<endl;
	else if(n==3) cout<<3<<endl;
	else if(n==4) cout<<5<<endl;
	else if(n==5) cout<<8<<endl;
	else if(n==6) cout<<13<<endl;
	else if(n==7) cout<<21<<endl;
	else if(n==8) cout<<34<<endl;
	else if(n==9) cout<<55<<endl;
	else if(n==10) cout<<89<<endl;
	else if(n==11) cout<<144<<endl;
	else if(n==12) cout<<233<<endl;
	else if(n==13) cout<<377<<endl;
	else if(n==14) cout<<610<<endl;
	else if(n==15) cout<<987<<endl;
	else if(n==16) cout<<1597<<endl;
	else if(n==17) cout<<2584<<endl;
	else if(n==18) cout<<4181<<endl;
	else if(n==19) cout<<6765<<endl;
	else if(n==20) cout<<10946<<endl;
	else if(n==21) cout<<17711<<endl;
	else if(n==22) cout<<28657<<endl;
	else if(n==23) cout<<46368<<endl;
	else if(n==24) cout<<75025<<endl;
	else if(n==25) cout<<121393<<endl;
	else if(n==26) cout<<196418<<endl;
	else if(n==27) cout<<317811<<endl;
	else if(n==28) cout<<514229<<endl;
	else if(n==29) cout<<832040<<endl;
	else if(n==30) cout<<1346269<<endl;
	else if(n==31) cout<<2178309<<endl;
	else if(n==32) cout<<3524578<<endl;
	else if(n==33) cout<<5702887<<endl;
	else if(n==34) cout<<9227465<<endl;
	else if(n==35) cout<<14930352<<endl;
	else if(n==36) cout<<24157817<<endl;
	else if(n==37) cout<<39088169<<endl;
	else if(n==38) cout<<63245986<<endl;
	else if(n==39) cout<<102334155<<endl;
	else if(n==40) cout<<165580141<<endl;
	else if(n==41) cout<<267914296<<endl;
	else if(n==42) cout<<433494437<<endl;
	else if(n==43) cout<<701408733<<endl;
	else if(n==44) cout<<1134903170<<endl;
	else if(n==45) cout<<1836311903<<endl;
	else if(n==46) cout<<2971215073<<endl;
	else if(n==47) cout<<4807526976<<endl;
	else if(n==48) cout<<7778742049<<endl;
	else if(n==49) cout<<12586269025<<endl;
	else if(n==50) cout<<20365011074<<endl;
	return 0;
}

个位是不是看得有点眼花缭乱了,再此,我真诚地向大家劝告“别用第二种方法”,这种方法既“龟速”,又“易错”,简直是“一箭双雕”“一石二鸟”。

好了,我要吃晚餐了,拜拜┏(^0^)┛。

AC

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值