#include <iostream> #include "windows.h" using namespace std; /*----------------------------------- 母牛生小牛Problem 设有一头小母牛,从出生第四年起每年生一头小母牛,按此规律,第N年时有几头母牛? Input 本题有多组数据。每组数据只有一个整数N,独占一行。(1≤N≤50) Output 对每组数据,输出一个整数(独占一行)表示第N年时母牛的数量 Sample Input 1 4 5 20 Sample Output 1 2 3 872 -----------------------------------*/ /*---------------------------------- 1 1 1 2 3 4 6 9 13 19 28 ... 后一项 等于 前x-1项 + x-3 之和 f(x) = f(x-1) + f(x-3) 特殊的斐波那契数列 -----------------------------------*/ int MuNiu(const int& value){ int sum[1000]; sum[0] = sum[1] = sum[2] = 1; for(int i = 3 ;i < value;i++){ sum[i] = sum[i - 1] + sum[i - 3]; } return sum[value-1]; } int MuNiu2(const int& value){ if(value <= 3){ return 1; }else{ return MuNiu2(value - 1) + MuNiu2(value - 3); } } int main(){ DWORD a = ::GetTickCount(); cout<<MuNiu(50)<<endl; DWORD b = ::GetTickCount(); DWORD c = b - a; cout<<c<<endl; a = ::GetTickCount(); cout<<MuNiu2(50)<<endl; b = ::GetTickCount(); c = b - a; cout<<c<<endl; return 0; }