求和 + 幸运口袋(DFS)

DFS
一条路走到黑

一、求和

1、题目描述
题目链接
在这里插入图片描述
在这里插入图片描述

2、题目分析
这是一个典型的DFS的题目

  1. 数字在一种组合中不能重复使用
  2. 一种组合之中数字是递增的
  3. 组合与组合之间按字典序排序

n 是 5
m 是 5
1 、2、3、4、5中任取几个数,加和是 5
在这里插入图片描述

3、代码

  1. 使用一个List<List<Integer>> lists 保存所有的组合
  2. 使用 List<Integer> list 保存当前 所加 的数字,result表示当前的和,value表示当前的数字,
  3. 在当前value的基础上,for循环遍历其后的 数字(value + 1, value + 2…) ;对每一个 新的value 进行DFS,DFS结束后,注意删除 list 中的上次添加的这个 新的value
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int n = scanner.nextInt();
            int m = scanner.nextInt();

            // 存放所有的组合
            List<List<Integer>> lists = new ArrayList<>();

            // 一种组合
            List<Integer> alist = new ArrayList<>();
            DFS43(lists, alist, 0, n, m, 0);

            for (List<Integer> list : lists) {
                for (int value : list) {
                    System.out.print(value + " ");
                }
                System.out.println();
            }
        }
    }

    public static void DFS(List<List<Integer>> lists, List<Integer> list, int result, int n, int m, int value) {

        // 如果当前的 和 > m,回溯
        if (result > m) {
            return;
        }

        // 如果当前的 和 == m,新建一个list添加到 lists中,不可添加原有的list
        if (result == m) {
            lists.add(new ArrayList<>(list));
            return;
        }

        // 继续往后加
        for (int i = value + 1; i <= n; i++) {
            // 添加 一个新的数字
            list.add(i);
            DFS(lists, list, result + i, n, m, i);
            // 删除 这个数字,加下一个数字
            list.remove(list.size() - 1);
        }
    }

二、幸运袋子

1、题目描述
题目链接

在这里插入图片描述
2、题目分析加解决

  1. 题目意思就是任取带号码的小球,组成幸运袋子(和 > 积),求一共有几种幸运袋子

在这里插入图片描述

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] a = new int[n];


        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }

        // 排序
        Arrays.sort(a);

        System.out.println(getCount(a, n, 0, 0, 1));
    }
    public static int getCount(int[] a, int n, int pos, int sum, int mul){
        int count = 0;
        for (int i = pos; i < n; i++) {
            int newSum = sum + a[i];
            int newMul = mul * a[i];
            if(newSum > newMul){
                count = count + 1 + getCount(a, n,i + 1, newSum, newMul);
            } else if(a[i] == 1){
                count = count + getCount(a, n, i + 1, newSum, newMul);
            } else {
                break;
            }
            while(i < n - 1 && a[i] == a[i + 1]){
                i++;
            }
        }
        return count;
    }
  1. 如果是从一个包含所有小球的袋子中,往外拿出小球,判断袋子中剩余的小球能否组成幸运袋子,看看一共有多少种幸运袋子
    无法剪枝,时间复杂度过高,测试不通过
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];

        int sum = 0;
        int mul = 1;
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
            sum += array[i];
            mul *= array[i];
        }

        Arrays.sort(array)
            

        int num = DFS(array, n, 0, sum, mul);
        if(sum > mul){
            num++;
        }
        System.out.println(num);
    }


    public static int DFS(int[] array, int n, int pos, int sum, int mul){
        int count = 0;
        for (int i = pos; i < n; i++) {
            int newSum = sum - array[i];
            int newMul = mul / array[i];
            if(newSum > newMul){
                count++;
            }
            count = count + DFS(array, n, i + 1, newSum, newMul);
            while(i < n - 1 && array[i] == (array[i + 1])){
                i++;
            }
        }
        return count;
    }

仙境海岸·鲜美烟台

山东烟台

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

威少总冠军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值