第一种: public class Cow { public static int count = 0; public Cow(int year){ count++; for(int i=3+year;i <=10;i++){ //计算在第I年出生的牛,直到第N年时生出了多少牛 new Cow(i); } } public static void main(String[] args) { new Cow(0); System.out.println(count); } } 第二种: #include"iostream" using namespace std; int Cow(int n) { if(n <0) return 0; int Count[3]={1,0,0}; //Count[0]为年龄为1的牛的数量,Count[1]为年龄为2的牛的数量 //Count[2]为年龄大于等于3的牛的数量 for(int i=0;i <n;i++) { int temp=Count[2];//temp为第i年生下的小牛数 Count[2]+=Count[1];//年龄加1 Count[1]=Count[0]; Count[0]=temp; } return Count[0]+Count[1]+Count[2]; } int main() { for(int i=1;i <=15;i++) cout < <"第" < <i < <"年的牛为" < <Cow(i) < <"头" < <endl; } 第三种: public class Cow { static int count = 1; private static void feedCow(int year,int age){ year++; age++; if(year<=30){ if(age>=3){ count++; feedCow(year,0); } feedCow(year,age); } } public static void main(String[] args) { new Cow().feedCow(0, 0); System.out.println(count); } }