【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21…
//导包
import java.util.Scanner;
public class bianchengti_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);System.out.print("请输入要查询的月份(整数):");int month = sc.nextInt();//定义一个数组用来存放每个月的兔子对数int []value = new int[month];value[0]=1;//注意数组长度超出问题if(month>=2){
value[1]=1;}int i;//循环计算month的兔子对数for(i=2;i<month;i++){
value[i] = value[i-1] + value[i-2];}System.out.print(month+"月的兔子为 "+value[month-1]+"对");}
}
重点在找出规律
独立思考哦!!
待进阶后再来完善