• 幼儿园分班


    说明:不喜欢的人不能分在一起,查询是否有解决方案

    思路:无法分班的情况是,当一个班级同时包含两个不喜欢的人时就无法分班

    代码:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashSet;
    
    public class Client {
    
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int n = Integer.parseInt(br.readLine());
    
            String[] strs = new String[n];
            for (int j = 0; j < n; j++) {
                strs[j] = br.readLine();
            }
            boolean suc = true;
            HashSet<String> class1 = new HashSet<>();
            HashSet<String> class2 = new HashSet<>();
            for (String str : strs) {
                int ticket = calc(str,class1,class2);
                if(ticket == 0){
                    suc = false;
                    break;
                }
            }
            if(suc){
                System.out.println(1);
            }else {
                System.out.println(0);
            }
    
        }
        private static int calc(String str, HashSet<String> class1, HashSet<String> class2) {
            String[] split = str.split(" ");
            String one = split[0];
            String two = split[1];
            if(class1.contains(one) && class1.contains(two)){
                return 0;
            }
            if(class2.contains(one) && class2.contains(two)){
                return 0;
            }
            class1.add(one);
            class2.add(two);
            return 1;
        }
    
    }
  • 相关阅读:
    正则表达式
    UVALive
    Python科学计算基础篇
    IntelliJ IDEA 2017.3激活与汉化
    hive order by,sort by, distribute by, cluster by作用以及用法
    Hive调优
    Hive 索引
    hive视图
    Hive 分区 分桶使用
    linux内核优化,内核参数详解
  • 原文地址:https://www.cnblogs.com/dongma/p/13430420.html
Copyright © 2020-2023  润新知