zoukankan      html  css  js  c++  java
  • 人口普查

    这题目用java是会超时的,我提供java代码,自己对照去写c++,用c++最后一个测试点100ms左右,估计用java需要500-600ms,会超时,一般200ms的用java能过的可能性就比较小了,倒数第二个测试点如果出现段错误就是你的数组越界了,没有考虑全部错误的情况,输出0,最后一个测试点数据有点大,如果是错误,就是放进容器时的判断条件有错。




    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Scanner;
    
    class Person {
        public int year, month, day;
        public String name;
        public Person(int year, int month, int day, String name) {
            this.year = year;
            this.month = month;
            this.day = day;
            this.name = name;
        }
    }
    
    public class Main {
    
        public static int year, month, day;
    
        public static void main(String[] args) {
            List<Person> list = new ArrayList<Person>();
            Scanner cin = new Scanner(System.in);
            int n = cin.nextInt(), count = 0;
            for (int i = 0; i < n; ++i) {
                String name = cin.next();
                String[] c = cin.next().split("/");
                year = Integer.parseInt(c[0]);
                month = Integer.parseInt(c[1]);
                day = Integer.parseInt(c[2]);
                if (isCheck()) {
                    list.add(new Person(year, month, day, name));
                    ++count;
                }
            }
            if (count > 0) {
                Collections.sort(list, new Comparator<Person>() {
    
                    @Override
                    public int compare(Person o1, Person o2) {
                        if (o1.year != o2.year) {
                            return o1.year - o2.year;
                        } else if (o1.month != o2.month) {
                            return o1.month - o2.month;
                        } else {
                            return o1.day - o2.day;
                        }
                    }
                });
                System.out.println(count + " " + list.get(0).name + " " + list.get(count-1).name);
            }
            else {
                System.out.println(0);
            }
        }
    
        public static boolean isCheck() {
            if (year > 2014 || year < 1814)
                return false;
            else if (year == 2014) {
                if (month > 9)
                    return false;
                else if (month == 9) {
                    if (day > 6)
                        return false;
                }
            } else if (year == 1814) {
                if (month < 9)
                    return false;
                else if (month == 9) {
                    if (day < 6)
                        return false;
                }
            }
            return true;
        }
    }
    

    ========================================Talk is cheap, show me the code=======================================

    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    Azure Bicep(三)变量控制
    FreeRedis分布式锁实现以及使用
    动态表单存储设计
    SixLabors.ImageSharp 实践小结
    你了解一条sql的执行顺序吗
    理解ASP.NET Core
    理解ASP.NET Core
    产品说,我只需要一个有亿点复杂的查询界面
    学习大数据可以考哪些证书(附资料)
    数据治理之元数据管理的利器——Atlas入门宝典
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179814.html
Copyright © 2011-2022 走看看