- 论坛徽章:
- 0
|
母牛数量算法
原帖由 "小飞爱使申华" 发表:
题目出的不清楚,这第四年算几岁啊 (这一出生算第几年啊),虚岁是4岁,实足是3岁,我是按实足3岁算的,如果要按实足4岁,把我的code中 i 的初始值由3改为4就行。
唉,搂住,你有什么公式,拿出来大伙瞧瞧,把..........
应该是这样的:在第四年初生一头,第四年初有两头,这样下去,每头牛在第四年初开始生第一头,大于第四年就继续生。
在第六年开始,第n年的头数为sum=born(k-1)+(k-5),意思就是第n-1年有a头,n有b头,n+1有c头,那c=b+(n+1-5),b=a+(n-5),
直接来说就是明年减去今年的数量比今年减去年的数量大1。
我用vc:
#include<iostream.h>;
long born( unsigned int k)
{
if(k<=5)
return 3;
else
{int sum;
sum=born(k-1)+(k-5);
return sum;
}
}
void main()
{
int n,a;
cin>;>;n;
a=born(n);
if(n<=3)
{int sum=1;
cout<<"when k<=3,there isonly "<<sum<<"cow!"<<endl;
}
else if(n==4||n==5)
{
int sum=n-2;
cout<<"when k==4||k==5,there is "<<sum
<<" cow!"<<endl;
}
else
cout<<"when the year is"<<n<<",the number of cow is:"
<<a<<endl;
}
如果要看它每一步的计算结果,如下:
#include<iostream.h>;
long born( unsigned int k)
{
if(k<=5)
return 3;
else
{int sum;
sum=born(k-1)+(k-5);
cout<<"when the year is"<<n<<",the number of cow is:"
<<a<<endl;
return sum;
}
}
void main()
{
int n,a;
cin>;>;n;
a=born(n);
if(n<=3)
{int sum=1;
cout<<"when k<=3,there isonly "<<sum<<"cow!"<<endl;
}
else if(n==4||n==5)
{
int sum=n-2;
cout<<"when k==4||k==5,there is "<<sum
<<" cow!"<<endl;
}
else
cout<<"when the year is"<<n<<",the number of cow is:"
<<a<<endl;
}
我算得n=100只是4563头,其实我也觉得不可能有上面这些人算得那么多,因为只是等差(公差只是每一年增加1)增加而已 |
|