Java递归练习

目录

1. 递归求N的阶乘

2. 按顺序打印一个数字的每一位

3. 递归求n的和

4. 计算一个数每一位之和

5. 求斐波那契数列的第N项

6. 汉诺塔

7. 青蛙跳台阶


 

1. 递归求N的阶乘

//递归求n的阶乘
public class test {
    //求n的阶乘方法
    public static int func(int n) {
        if (n == 1) {
            return 1;
        }
        int tmp = n * func(n-1);
        return tmp;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(func(n));
    }
}

2. 按顺序打印一个数字的每一位

例如1234打印出1 2 3 4

//按顺序打印一个数字的每一位
public class test {
    //顺序打印数字
    public static void func(int num) {
        if (num > 9) {
            func(num / 10);
        }
        System.out.print(num % 10 + " ");
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        func(num);
    }
}

3. 递归求n的和

//递归求n的和
public class test {
    //
    public static int  func(int num) {
        if (num == 1) {
            return 1;
        }
        int sum = num + func(num -1);
        return sum;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println(func(num));
    }
}

4. 计算一个数每一位之和

输入一个非负数,返回组成它的数字之和。例如,输入1729,则应该返回1+7+2+9,它的和是19

//计算一个数每一位之和
public class test {
    //
    public static int sum(int num) {
        if (num < 10) {
            return num;
        }
        return num%10 + sum(num/10);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println(sum(num));
    }
}

5. 求斐波那契数列的第N项

//求斐波那契数列的第N项
public class test {
    //递归方式
    public static int fib1(int n) {
        if (n ==1 | n == 2) {
            return 1;
        }
        return fib1(n-1) + fib1(n-2);
    }
    //迭代方式(非递归)
    public static int fib2(int n) {
        if (n == 1 || n ==2) {
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(fib1(n));
    }
}

6. 汉诺塔

//汉诺塔问题
public class test {
    //打印移动的情况
    public void move(char pos1, char pos2) {
        System.out.println(pos1+"->"+pos2+" ");
    }
    //hanoiTower,n为当前盘子个数,pos1为起始位置,pos2中转位置,pos3目的位置
    public static void hanoiTower(int n, char pos1, char pos2, char pos3) {
        if(n == 1) {
            move(pos1, pos2);
        } else {
            hanoiTower(n-1, pos1,pos3,pos2);
            move(pos1,pos3);
            hanoiTower(n-1,pos2,pos1,pos3);
        }
    }
    public static void main(String[] args) {
        hanoiTower(1,'A','B','C');    // A->B
        System.out.println();
        hanoiTower(2,'A','B','C');    // A->C A->C B->A
        System.out.println();
        hanoiTower(3,'A','B','C');    // A->B A->B C->A A->C B->C B->C A->B
        System.out.println();
    }
}

7. 青蛙跳台阶

斐波那契数列的变形,第一个是1,第二个是2(斐波那契前两个为1)

//青蛙跳台阶问题
public class test {
    public static int jumpFloor(int target) {
        if (target == 1) {
            return 1;
        }
        if (target == 2) {
            return 2;
        }
        return jumpFloor(target - 1) + jumpFloor(target - 2);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(jumpFloor(n));
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值