C++问题,求高手帮忙

来源:百度知道 编辑:UC知道 时间:2024/06/24 08:32:33
编程求解:若一只小母羊,从出生起第三个年头开始每年生一只母羊,按此规律,求第n年小母羊的个数。
求高手编一个c++程序来解决这个问题。

中间的可生育与不可生育羊数 根据带入简单值观察出部分量相等进行了替换以简化程序 以下是个人分析出的算法

#include <iostream>
using namespace std;

int main()
{
int year;
cout<<"输入年数:";
cin>>year;
if(year<3) cout<<1;

else
{
int *a//可生育的
,*b;//不可生育的

a=new int[year];
b=new int[year];

a[0]=0;a[1]=0; //前两年 可生育 0 不可生育 1
b[0]=1;b[1]=1;
int i=2;//从第3年开始
for(;i<year;i++)
{
a[i]=a[i-2]+b[i-2];
b[i]=a[i-1]+b[i-1];

}
cout<<a[i-1]+b[i-1];

delete []a;
delete []b;
}

cout<<"头羊"<<endl;

return 0;
}

#include<iostream>
using namespace std;

int main()
{
int n;
cin >> n;
cout << n / 3 << endl;//不算第一只
cout << n / 3 + 1 << endl; //算第一只
return 0;
}

#in