一个农夫养了一头牛,三年后,这头牛每年会生出
1
头牛,生出来的牛三年后,又可以每年生出一头牛
……
问农夫
10
年后有多少头牛
?n
年呢
?
假设牛不死亡的话, 10 年后有 28 头牛。
package dailyTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test006 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(" 你要查询几年后牛的总数 :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
try {
n = Integer.parseInt(br.readLine());
int x = 1, y = 1;
for(int i=0;i<n;i++) {
if(i>=3) { y = x + y;
x = y - x;
}
}
System.out.println(n + " 年后总共有 " + y + " 头牛 ");
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
假设牛不死亡的话, 10 年后有 28 头牛。
package dailyTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test006 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(" 你要查询几年后牛的总数 :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
try {
n = Integer.parseInt(br.readLine());
int x = 1, y = 1;
for(int i=0;i<n;i++) {
if(i>=3) { y = x + y;
x = y - x;
}
}
System.out.println(n + " 年后总共有 " + y + " 头牛 ");
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}