题目:印度蜜蜂,每年每只雄蜂产下一只雌蜂和一只雄蜂,每只雌蜂产下一只雄蜂,然后就死去;
现在发现了一只不会死的雌蜂,问以她为起始点,第N年有多少雄蜂和一共多少蜜蜂。
一年后 雄蜂1 雌蜂1
两年后 雄蜂2 1+1+1-1 雌蜂2 1+1
三年后 雄蜂4 雌蜂30 1 2 3 4 5 6
雄 0 1 2 4
雌 1 1 2 3
总 1 2 4 7
a[i]=c[i-1]+a[i-1]-a[i-1];
b[i]=a[i-1]+b[i-1]-b[i-1]+1;
c[i]=a[i]+b[i];
由以上三式可得c[i]=c[i-1]+c[i-2]+1;
//没有提交到UVa平台验证代码
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin>>n && n!=-1)
{
long long int a[100],b[100],c[100];//使用long long类型防止溢出
a[0]=0;b[0]=1;c[0]=1;
for(int i=1;i<=n;i++)
{
a[i]=c[i-1];
b[i]=a[i-1]+1;
c[i]=a[i]+b[i];
}
cout<<a[n]<<" "<<c[n]<<endl;
}
return 0;
}