- 論壇徽章:
- 0
|
母牛數(shù)量算法
原帖由 "小飛愛使申華" 發(fā)表:
題目出的不清楚,這第四年算幾歲啊 (這一出生算第幾年啊),虛歲是4歲,實(shí)足是3歲,我是按實(shí)足3歲算的,如果要按實(shí)足4歲,把我的code中 i 的初始值由3改為4就行。
唉,摟住,你有什么公式,拿出來大伙瞧瞧,把..........
應(yīng)該是這樣的:在第四年初生一頭,第四年初有兩頭,這樣下去,每頭牛在第四年初開始生第一頭,大于第四年就繼續(xù)生。
在第六年開始,第n年的頭數(shù)為sum=born(k-1)+(k-5),意思就是第n-1年有a頭,n有b頭,n+1有c頭,那c=b+(n+1-5),b=a+(n-5),
直接來說就是明年減去今年的數(shù)量比今年減去年的數(shù)量大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;
}
如果要看它每一步的計算結(jié)果,如下:
#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頭,其實(shí)我也覺得不可能有上面這些人算得那么多,因?yàn)橹皇堑炔睿ü钪皇敲恳荒暝黾?)增加而已 |
|