[Java] 纯文本查看 复制代码 import java.util.Scanner;
class BuSiTu {
/*
【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,
问每个月的兔子对数为多少?
*/
/*
1,1,2,3,5,8,13,...((n - 1) + (n - 2))
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
print();
while (true) {
System.out.print("继续输入请按0,退出输入请按1:");
int s = sc.nextInt();
System.out.println();
if (s == 0) {
print();
}else if(s == 1) {
break;
}
}
}
public static void print() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入月份:");
int c = sc.nextInt();
System.out.println();
int b = tuzi(c);
if (b == 1) {
System.out.println("第" + c + "个月的兔子对数为:" + b + "对。" + "\r\n");
}else {
System.out.println("第" + c + "个月的兔子对数为:" + b + "对。" + "\r\n");
}
}
public static int tuzi(int a) {
if (a == 1 || a == 2) {
return 1;
}
return tuzi(a - 1) + tuzi(a - 2);
}
}
自己做的,你可以参考一下,有什么更好的建议可以讨论讨论! |