zoukankan      html  css  js  c++  java
  • PTA 天梯赛 L2_021

    考察自定义结构体的排序

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.HashSet;
    import java.util.Set;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {  // 自定义结构体排序
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            int N = Integer.parseInt(bf.readLine());
    //        int[][] list = new int[N][2];
    //        String[] name = new String[N];
            Node[] nlist = new Node[N];
            Set<String> set = new HashSet<String>();
            for (int i = 0; i < N; i++) {
                set.clear();
                String[] s = bf.readLine().split(" ");
    //            name[i] = s[0]; // 输入的名字
                int times = Integer.parseInt(s[1]);
    //            list[i][1] = times; // 总共输入的数字
                for (int j = 2; j < s.length; j++) {
                    set.add(s[j]);
                }
    //            list[i][0] = set.size(); // 不重复的标签的数量
                nlist[i] = new Node(s[0], set.size(), times);
            }  // 完成输入的功能
    //        for(Node e: nlist) {
    //            System.out.println(e.name + " " + e.setLen + " " + e.allLen);
    //        }
    //        System.out.println();
            Arrays.sort(nlist, new cmp());
            
    //        for(Node e: nlist) {
    //            System.out.println(e.name + " " + e.setLen + " " + e.allLen);
    //        }
            
            if(nlist.length >= 3) {
                for(int i=nlist.length-1; i>=nlist.length-3; i--) {
                    if(i == nlist.length-1) {
                        System.out.print(nlist[i].name);
                    } else {
                        System.out.print(" "+nlist[i].name);
                    }
                }
            } else {
                if(nlist.length == 2) {
                    for(int i=nlist.length-1; i>=nlist.length-2; i--) {
                        if(i == nlist.length-1) {
                            System.out.print(nlist[i].name);
                        } else {
                            System.out.print(" "+nlist[i].name);
                        }
                    }
                    System.out.print(" " + "-");
                } else if(nlist.length == 1) {
                    System.out.print(nlist[0].name + " - -");
                }
            }
        }
        
        static class cmp implements Comparator<Node>{
            // 降序排列
            public int compare(Node A, Node B) {
                if (A.setLen == B.setLen) {
                    if (A.allLen < B.allLen)
                        return 1;
                    else
                        return -1;
                }
                if (A.setLen > B.setLen)
                    return 1;
                else
                    return -1;
            }
        }
        
        static class Node {
            String name;
            int setLen;
            int allLen;
            Node(String name, int setLen, int allLen) {
                this.name = name;
                this.setLen = setLen;
                this.allLen = allLen;
            }
            
        }
    
    }

    最后两个点超时了.

  • 相关阅读:
    使用 Hudson 进行持续集成
    hudson中的ftp插件
    Tomcat i18n 对 URL 进行 UTF8 解码
    Product deploy using NAnt and NSIS [技术点滴]
    切身体会到对微软的无语——关于VS2008的配置项
    How to create your own home SVN repository with Xampp/Apache
    CVS+VS2003+SetupFactory建设每日构建(Daily build)
    期待 JDK、Tomcat和MySQL的绿色安装方法
    FolderBrowserDialog 使用时路径问题
    无敌删除命令
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10612547.html
Copyright © 2011-2022 走看看