暴力递归和动态规划

暴力递归和动态规划

求n!的结果

  1. 思路:要求n!,则要先求出(n-1)!,n*(n-1)! 即为n!的结果。要求(n-1)!,则要先······
  2. 代码
    public class Factorial {
        public static int factorial(int num) {
            if (num == 1) {
                return 1;
            }
            return factorial(num - 1) * num;
        }
    
        public static void main(String[] args) {
            System.out.println(factorial(3));
        }
    }
    
    

汉诺塔问题

  1. 题目:打印n层汉诺塔从最左边移动到最右边的全部过程
  2. 思路:分三步
  • 先将1~n-1层从左边移到中间
  • 再将第n层从左边移到最右边
  • 最后再将1~n-1层从中间移到最右边
  1. 代码
    /**
     * 汉诺塔问题
     */
    public class Hanoi {
        public static void hanoi(int N, String from, String to, String help) {
            if (N == 1) {
                System.out.println("move 1 from" + from + "to" + to);
            } else {
                hanoi(N-1,from,help,to);
                System.out.println("move"+ N +"from"+from+"to"+to);
                hanoi(N-1,help,to,from);
            }
        }
    
    
        public static void main(String[] args) {
            hanoi(3,"左","右","中");
        }
    }
    
    

打印一个字符串的全部子序列,包括空字符串

  1. 思路:从字符串的第一个位置开始,每次遍历到某一个位置时,该位置都有两种取值情况,一种是取该位置字符,另一种取空。
  2. 代码:
    
    public class PrintAllSubsquences {
    
        /**
         * 打印一个字符串的全部子序列,包括空字符串
         * @param str
         */
        public static void printAllSubsquences(String str) {
            char[] chars = str.toCharArray();
            process(chars,0,"");
        }
    
        public static void process(char[] chars,int i,String res) {
            if (i == chars.length) {
                System.out.println(res);
                return;
            }
            process(chars,i+1,res);
            process(chars,i+1,res+chars[i]);
        }
    
    
        public static void main(String[] args) {
            String test = "abc";
            printAllSubsquences(test);
        }
    }
    
    

母牛每年生一只母牛,新出生的母牛成长三年后也能每年生一只母牛,假设不会死。求N年后,母牛的数量。

  1. 思路:当年数大于3年时,某一年牛的数量=3年前牛的数量+一年前牛的数量,因为新生的小牛3年后才能生小牛。
  2. 代码
    
    /**
     * 母牛每年生一只母牛,新出生的母牛成长三年后也能每年生一只
     * 母牛,假设不会死。求N年后,母牛的数量。
     */
    
    public class Cow {
        public static int cowNum(int year) {
            if (year < 0) {
                return 0;
            }
            if (year == 1 || year == 2 || year == 3) {
                return year;
            } else {
                return  cowNum(year - 1) + cowNum(year - 3);
            }
    
        }
    
    
        public static void main(String[] args) {
            System.out.println(cowNum(20));
        }
    }
    
    

给你一个数组arr,和一个整数aim。如果可以任意选择arr中的数字,能不能累加得到aim,返回true或者false

  1. 思路:类似于求所有子序列问题。
  2. 代码
    
    public class IsSum {
    
        public static boolean isSum(int[] arr,int sum, int aim,int i) {
            if (i==arr.length) {
                return sum==aim;
            }
            return isSum(arr, sum, aim, i + 1) || isSum(arr,sum+arr[i],aim,i+1);
        }
    
        public static void main(String[] args) {
            int[] arr = { 1, 4, 8 };
            int aim = 5;
            System.out.println(isSum(arr, 0,aim,0));
    
        }
    }
    
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值