母牛的故事(c++递归过不了)

题目地址:https://www.dotcpp.com/oj/problem1004.html?sid=1755437&lang=1#editor

c语言写递归能过,c++递归过不了。

c++写非递归能过,时间慢。

 

#include<iostream>
using namespace std;
int a[100]={0};
int solve(int year)
{
    for(int i=1;i<=year;i++)
    {
        if(i<=4)
        {
            a[i]=i;
        }
        else
        {
            a[i]=a[i-3]+a[i-1];
        }
    }
    return a[year];
}


int main()
{
    int n;
    while(cin>>n && n!=0)
    {
        cout<<solve(n)<<endl;
    }
}
posted @ 2020-01-13 21:22  张达嘛  阅读(459)  评论(0编辑  收藏  举报