2018 母牛的故事
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2018
递归问题
cow(i)表示第 i 年母牛数量
cow(i)=cow(i-1)+(i-3)
即前一年牛的基础上再加上三年前(现在是第四年)母牛数量
记得看《数据结构计算法分析》中说过递归造成的内存浪费,过两天请教大牛吧。
#include<iostream> using namespace std; int cow(int n) { if(n <= 4 ) return n; else return cow(n-1)+cow(n-3); } int main() { int n; while( cin >> n && n != 0 ) cout<<cow(n)<<endl; return 0; }