动态规划用数组保存结果,避免重复计算
吃糖果
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;
}
查看11道真题和解析
