從農夫養牛問題推廣到斐波那契數列
阿新 • • 發佈:2019-01-30
using System;
namespace TopCoder
{
class Program
{
static void Main(string[] args)
{
FeedCow feedCow = new FeedCow();
for (int i = 1; i < 30; ++i)
{
int sum = feedCow.Feed(i, 5, 7);
Console.WriteLine("{0}:{1}
}
}
}
///<summary>//////</summary>///<param name="year">要計算的年限</param>///<param name="age">牛的生育年齡</param>///<param name="deadAge">牛的死亡年齡</param>///<returns></returns>
public int Feed(int year, int age, int deadAge)
{
int
int min = deadAge < year ? deadAge : year;
for (int i = age; i <= min; ++i)
{
sum += Feed(year - i + 1, age, deadAge);
}
return sum;
}