蓝桥杯刷题记录之数字王国之军训排队

记录

卡了半天,check函数中的temp % ele ==0写成了ele % temp == 0就挺无语的

思路

这个晚上在补

代码

import java.util.*;
public class Main{
    static List<List<Integer>> que = new ArrayList<>();
    static int MIN = Integer.MAX_VALUE;
    static int[] people;
    public static void dfs(int index){
        if(index==people.length)
        {
            MIN = Math.min(MIN,que.size());
            return;
        }
        if(index>=MIN)
            return;
        int temp = people[index];
        for(int i=0;i<que.size();i++){
            //能加入队伍
            List<Integer> list = que.get(i);
                if(check(list,temp)){
                    list.add(temp);
                    dfs(index+1);
                    list.remove(list.size()-1);

                }
        }
        // 自立门户
        List<Integer> item =new LinkedList<>();
        item.add(temp);
        que.add(item);
        dfs(index+1);
        que.remove(que.size()-1);

    }

    private static boolean check(List<Integer> list, int temp) {
        for(Integer ele: list){
            if(temp % ele ==0)
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        people = new int[n];
        for(int i=0;i<n;i++){
            people[i] = s.nextInt();
        }
        Arrays.sort(people);
        List<Integer> item = new LinkedList<>();
        item.add(people[0]);
        que.add(item);
        dfs(1);
        System.out.println(MIN);
        s.close();
    }
}

相关推荐

  1. 记录数字王国军训排队

    2024-03-22 20:40:03       19 阅读
  2. 记录王国

    2024-03-22 20:40:03       21 阅读
  3. 记录质数

    2024-03-22 20:40:03       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 20:40:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 20:40:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 20:40:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 20:40:03       20 阅读

热门阅读

  1. Uni-app开发介绍及入门

    2024-03-22 20:40:03       18 阅读
  2. CUDA学习笔记07:shared memory Code

    2024-03-22 20:40:03       23 阅读
  3. [C++提高编程](二):模板--类模板

    2024-03-22 20:40:03       21 阅读
  4. CCF软件能力认证202312-1——仓库规划

    2024-03-22 20:40:03       20 阅读
  5. 5.3、【AI技术新纪元:Spring AI解码】图像生成API

    2024-03-22 20:40:03       18 阅读
  6. 树形el-select封装

    2024-03-22 20:40:03       19 阅读
  7. Element Plus 文本域设置固定行数

    2024-03-22 20:40:03       18 阅读