动态规划用数组保存结果,避免重复计算

吃糖果

http://www.nowcoder.com/questionTerminal/72015680c32b449899e81f1470836097

#include<iostream>
#include<cstdio>

using namespace std;
const int maxn=1000;
long long Chocolate[maxn];
int main()
{
	int n;
	Chocolate[0]=0;
	Chocolate[1]=1;
	Chocolate[2]=2;
	for(int i=3;i<=maxn;++i)
		{
			Chocolate[i]=Chocolate[i-1]+Chocolate[i-2];
		}
	while(cin>>n)
	{
		cout<<Chocolate[n]<<endl;
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务