水题中碰到递归,这让我情何以堪。
不过是有规律,从第四年起,牛的数目等于前一年加上前三年,不过这规律是抄袭的!
抄袭地址:http://blog.csdn.net/ysc504/article/details/8251349
#include<iostream>
#include<string>
using namespace std;
int cow(int n){
if(n==1)
return 1;
else if(n==2)
return 2;
else if(n==3)
return 3;
else
return cow(n-1)+cow(n-3);
}
int main(){
int n;
while((cin>>n) && n !=0){
cout<<cow(n)<<endl;
}
return 0;
}
总结:
1.规律这东西,找到了就递归吧,哈哈,已经有了递归的大致思路解法,下次碰到试一下。