假设牛不死亡的话, 10 年后有 55 头牛。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* Copyright (c) 2001-2010 ,Ltd. All Rights Reserved.
*/
/**
* <p>Title: .Cow.java</p>
* <p>Description: the Cow class</p>
* <p>Company: ghming1989@126.com</p>
* <p>@author hmguan</p>
* <p>@version $ Id:Cow.java 2010-12-23 hmguan$</p>
*/
public class Cow {
/**
* @param args
*/
public static void main(String[] args) {
Cow cow = new Cow();
int year ;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
year = Integer.parseInt(br.readLine());
System.out.println(year+" year later,the number cow is " + cow.CalCow(year));
} catch (IOException e) {
System.out.println("数据读取错误!");
}
}
public Cow() {
}
public int CalCow(int year){
int bigCow = 0 , midCow = 0 , littleCow = 1;
if(year < 1){
System.out.println("输入年份出错!");
return 0;
}
else{
for(int i = 1; i < year ;i++){
int temp = littleCow;
littleCow = bigCow + midCow ;
bigCow = bigCow + midCow ;
midCow = temp ;
}
}
return (bigCow + midCow + littleCow);
}
}