递归调用

输入日期,计算日期在本年的天数

public class Test01 {
 public static void main(String[] args) {
 // 输入
 Scanner sc = new Scanner(System.in);
 int year = 0,month = 0,date = 0;
 while (true) {
 year = inputNum(sc, 1, 3000, "年份");
 month = inputNum(sc, 1, 12, "月份");
 date = inputNum(sc, 1, 31, "日期");
 boolean bb = validateDate(year, month, date);
 if (bb) break;
 System.out.println("请输入合法的年月日!");
 }
 System.out.println(year+"-"+month+"-"+date);
 // 计算
 // 输出
 }
 public static boolean validateDate(int year, int month, int date) {
 boolean res = false;
 switch (month) {
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12:
 res = date <= 31 && date>=1;
 break;
 case 4:
 case 6:
 case 9:
 case 11:
 res = date <= 30 && date>=1;
 break;
 case 2:
 boolean bb = run(year);
 if (bb) res = date <= 29 && date>=1;
 else res = date <= 28 && date>=1;
 break;
 default:
 res = false;
 break;
 }
 return res;
 }
 public static boolean run(int year) {
 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  }
 public static int inputNum(Scanner sc, int min, int max, String str) {
 int res = 0;
 while (true) {
 System.out.println(str + ":");
 String ss = sc.nextLine();
 try {
 res = Integer.parseInt(ss);
 if (res >= min && res <= max)
 break;
 System.out.println("请输入合理的" + str + "数据");
 } catch (Exception e) {
 System.out.println("输入" + str + "数据不合法!");
 }
 }
 return res;
 }
}

计算阶乘5

public class Test04 {
 public static void main(String[] args) {
 int res=jie(5);
 System.out.println("5!="+res);
 }
 public static int jie(int k) {  //自己直接调用自己---递归调用
 if(k==0)
 return 1;
 return k*jie(k-1);
 }
}

斐波那契数列Fibonacci sequence,又称黄金分割数列,以兔子繁殖为例子而引入,故又称为“兔子数
列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34… [手写代码]
以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=3,n∈N*)

public class Test05 {
 public static void main(String[] args) {
 int months = 0;
 Scanner sc = new Scanner(System.in);
 while (true) {
 System.out.println("月份:");
 String ss = sc.nextLine();
 try {
 months = Integer.parseInt(ss);
 if (months > 0) break;
 System.out.println("请重新输入月份数!");
 } catch (Exception e) {
 System.out.println("请输入合法的月份数!");
 }
 }
 int num = tongji(months);
 System.out.println(months + "月后的兔子数为:" + num);
 }
 public static int tongji(int months) {
 if (months > 0) {
 if (months == 1 || months == 2) return 1;
 return tongji(months - 1) + tongji(months - 2);
 }
 return 0;
 }
}

有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13… 求出这个数列的前20项之和

public class Test06 {
 public static void main(String[] args) {
 double res = 0;
 for (int i = 1; i <= 20; i++) {
 res += 1. * diguiFenzi(i) / diguiFenmu(i);
 }
 System.out.println(res);
 }
 public static int diguiFenzi(int n) {
 // 如果使用递归调用必须保证有退出递归的点,必须保证不断逼近退出的点
 if (n == 1) return 2;
 else if (n == 2) return 3;
 else return diguiFenzi(n - 1) + diguiFenzi(n - 2);
 }
 public static int diguiFenmu(int n) {
 if (n == 1 || n == 2) return n;
 else return diguiFenmu(n - 1) + diguiFenmu(n - 2);
 }
}

梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆
盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆
盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘
移动次数是f(n).显然f(1)=1,f(2)=3,f(3)=7,且f(k+1)=2*f(k)+1


```java
public class 汉诺塔问题 {
 public static void main(String[] args) {
 System.out.println(move(3));
 }
 public static long move(int n) {
 if (n == 1)
 return 1;
 return 2 * move(n - 1) + 1;
 }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值