zoukankan      html  css  js  c++  java
  • 1028 人口普查 (20分)


    (color{#FF0000}{教训})

    (color{#FF00FF}{不要用Date来计算两个Date相差的年份。})

    (color{#FF7D00}{ 例如:1814/09/06 2014/09/06 最初我以为是相差天数就是20*365(但是忽略的闰年366天的存在) 所以很多博客中都只提到了两个日期相差ms、s、min、hour、day})

    ms=date1.getTime()-date2.getTime();
    s=ms/1000;    (date1.getTime()-date2.getTime())/(1000);
    min=s/60;     (date1.getTime()-date2.getTime())/(1000*60); 
    hour=min/60;  (date1.getTime()-date2.getTime())/(1000*60*60);
    day=hour/24;  (date1.getTime()-date2.getTime())/(1000*60*60*24); 
    year=day/365;//这是错的
    

    如果要计算相差年份可以将其转为字符串,判断相差年份是否超过n年

    bug代码:

    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) throws ParseException, IOException {
            BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy/mm/dd");
           // long l = (sdf.parse("2014/09/06").getTime() - sdf.parse("1814/09/06").getTime()) / (1000 * 3600 * 24);
    //        System.out.println("相差天数:"+l);
    //        String nowStr="2014/09/06";
    //        String beforeStr="2014/09/16";
    //        long l = (sdf.parse(beforeStr).getTime() - sdf.parse(nowStr).getTime()) / (1000 * 3600 * 24);
    //        System.out.println(l);
            String nowStr="2014/09/06";
            Date now=sdf.parse(nowStr);
            int cnt=0;
            Map<Date,String> map= new TreeMap<>();
            int n=Integer.parseInt(sc.readLine());
            for(int i=0;i<n;i++){
                String []s=sc.readLine().split(" ");
                String value=s[0];
                String key=s[1];
               // System.out.println(key);
                String []str=key.split("/");
                if(Integer.parseInt(str[0])>2014){
                    continue;
                }
                //1000 * 24 * 60 * 60
    //            long time=now.getTime()-sdf.parse(key).getTime();//ms
    //            if((time/ (1000*3600*24)>365*200)){
    //                System.out.println("相差天数: "+time/ (1000*3600*24));
    //                continue;
    //            }
                if(Integer.parseInt(str[0])<1814){
                    continue;
                }
                if(Integer.parseInt(str[0])==1814){
                    if(Integer.parseInt(str[1])<9){
                        continue;
                    }
                    if(Integer.parseInt(str[1])==9){
                        if(Integer.parseInt(str[2])<6){
                            continue;
                        }
                    }
                }
                map.put(sdf.parse(key),value);
                cnt++;
            }
            if(map.size()==0){
                System.out.println("0");return;
            }
    
            int num=0;
            System.out.print(cnt+" ");
            for(Map.Entry<Date,String> m:map.entrySet()){
                Date key=m.getKey();
                String value=m.getValue();
                num++;
                if(num==1){
                    System.out.print(value+" ");
                }
                if(num==cnt){
                    System.out.println(value);
                   // return;
                }
                //System.out.println(sdf.format(key)+" :"+value);
            }
    
    
        }
    }
    
    
    
    

    两个测试点没过:

    
    
    //打印沙漏
    //an=2n-1;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) throws ParseException, IOException {
            BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy/mm/dd");
            String nowStr="2014/09/06";
            Date now=sdf.parse(nowStr);
            int cnt=0;
            Map<Date,String> map= new TreeMap<>();
            int n=Integer.parseInt(sc.readLine());
            for(int i=0;i<n;i++){
                String []s=sc.readLine().split(" ");
                String value=s[0];
                String key=s[1];
               
                String []str=key.split("/");
                //年份比现在还晚
                if(Integer.parseInt(str[0])>2014){
                    continue;
                }
                if(Integer.parseInt(str[0])==2014){
                    if(Integer.parseInt(str[1])>9){
                        continue;
                    }
                    if(Integer.parseInt(str[1])==9){
                        if(Integer.parseInt(str[2])>6){
                            continue;
                        }
                    }
    
    
                }
                //年龄相差大于200
                if(Integer.parseInt(str[0])<1814){
                    continue;
                }
                if(Integer.parseInt(str[0])==1814){
                    if(Integer.parseInt(str[1])<9){
                        continue;
                    }
                    if(Integer.parseInt(str[1])==9){
                        if(Integer.parseInt(str[2])<6){
                            continue;
                        }
                    }
                }
                map.put(sdf.parse(key),value);
                cnt++;
            }
            if(map.size()==0){
                System.out.println("0");return;
            }
    
            int num=0;
            System.out.print(cnt+" ");
            for(Map.Entry<Date,String> m:map.entrySet()){
                Date key=m.getKey();
                String value=m.getValue();
                num++;
                if(num==1){
                    System.out.print(value+" ");
                }
                if(num==cnt){
                    System.out.println(value);
                }
              
            }
    
    
        }
    }
    
    
    

    判断年龄是否合法的代码其实可以很简化:

                if(key.compareTo("2014/09/06")>0||key.compareTo("1814/09/06")<0){
                    continue;
                }
    

    一个测试点没过:

    不一样的烟火
  • 相关阅读:
    【Azure 应用服务】App Service中运行Python 编写的 Jobs,怎么来安装Python包 (pymssql)呢?
    【Azure 存储服务】使用POST方式向Azure Storage Queue中插入Message的办法
    【Azure Developer】使用Azure Resource Graph的查询语法的示例
    img标签到底是行内元素还是块级元素
    [前端面试]前端缓存问题看这篇,让面试官爱上你
    Vue3.0 响应式数据原理:ES6 Proxy
    几行代码教你解决微信生成海报及二维码
    冷门的HTML
    HTML选择器
    程序猿必备 代码保平安
  • 原文地址:https://www.cnblogs.com/cstdio1/p/12130977.html
Copyright © 2011-2022 走看看