题解 | #[NOIP2016]回文日期#

[NOIP2016]回文日期

https://ac.nowcoder.com/acm/problem/16438


import java.util.Scanner;
public class Main{
    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        int start = Integer.parseInt(str1.substring(0, 4));
        int end = Integer.parseInt(str2.substring(0, 4));
        int sum = 0;
        for(int i=start;i<=end;i++){
            if(judge(i+"")){
                sum +=1;
            }
        }
        System.out.println(sum);
    }
    
    public static boolean judge(String s){
        int [] biaozhi = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int year = Integer.parseInt(s);
        char []ch = s.toCharArray();
        String riqi = "";
        for(int i = 3;i>=0;i--){
             riqi+=ch[i];
        }
        int month = Integer.parseInt(riqi.substring(0, 2));
        int day = Integer.parseInt(riqi.substring(2, 4));
        if(month>0&&month<13){
        if(month==2){
          if((year%4==0&&year%100!=0)||year%400==0){
            biaozhi[2] =29;
          }else{
            biaozhi[2] = 28;
          }  
        }
        if(day>=0&&day<=biaozhi[month]){
            return true;
        }
        
    }
        return false;
    
    }
}
全部评论

相关推荐

11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务