Fancy Mouse
- -|||
类似Fibonacci数列,记得这种题用递归做你就死翘翘了(复杂度O(2^N))。好歹也应该设一个数组再递推的(复杂度O(N))
关于Fibonacci数列,TJU1248的难度……偶现在还是tle……努力思索中,哈哈~~
帖1005的代码:
#include<iostream>
using namespace std;

int Cows(int Years);
int main()
{
    
int yrs,cow;
    
while(cin>>yrs)
    
{
        cow
=Cows(yrs);
        cout
<<cow<<endl;
    }

    
return 0;
}


int Cows(int Years)
{
    
if(Years<4return 1;
    
else
    
{
        
int temp=4;
        
int cow[]={1,1,1,1};
        
for(;temp<=Years;temp++)
            cow[temp
%4]=cow[(temp-1)%4+ cow[(temp-3)%4];
        
return cow[Years%4];
    }

}

posted on 2005-08-21 12:12  Fancy Mouse  阅读(656)  评论(4编辑  收藏  举报