传智杯练习赛(儒略历)

题目描述

在 1582 年之前,以 4 为倍数的年份为闰年。正常情况下,一年中一月到十二月的天数分别是 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 天。如果这年是闰年,那么二月则有 29 天。

但某位教皇发现这么做其实不够准确,会造成误差,因此规定从 1582 年开始,以 4 为倍数的年份,除了以 100 为倍数且不为 400 的倍数年份,才是闰年。同时为了消除误差,规定 1582 年 10 月 4 日的下一天是 1582 年 10 月 15 日,中间的日期就当作不存在了。

现在给出日期,计算这个日期到公元 1 年 1 月 1 日经过的天数。

输入格式

按照 日月年 的格式输入数据,其中日是 1 到 31 之间的整数,月是三个大写字母,年是 1 到 9999 之间的整数。保证这个日期是合法且存在的。

月份的大写字母:

  • 1月:JAN
  • 2月:FEB
  • 3月:MAR
  • 4月:APR
  • 5月:MAY
  • 6月:JUN
  • 7月:JUL
  • 8月:AUG
  • 9月:SEP
  • 10月:OCT
  • 11月:NOV
  • 12月:DEC

输出格式

输出一个整数表示答案

输入输出样例

输入

1JAN1

输出

0

输入 

4OCT1582

输出 

577736

输入 

15OCT1582

输出 

577747

输入 

21NOV2020

输出 

737751
import java.util.*;
public class Main {
	static int[] days=new int[] {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public static void main(String args[]) {
        Scanner scan=new Scanner(System.in);
        String str=scan.next();
        int biaoji=0;
        int day=0;
        String month1="";
        int year=0;
        int count=0;
        for(int i=0;i<str.length();i++) {
            if(biaoji==0&&str.charAt(i)>='0'&&str.charAt(i)<='9') {
                day=day*10+str.charAt(i)-'0';
            }
            if(biaoji==0&&str.charAt(i)>='A'&&str.charAt(i)<='Z') {
                month1+=str.charAt(i);
            }
            if(month1.length()==3) {
                biaoji=1;
            }
            if(biaoji==1&str.charAt(i)>='0'&&str.charAt(i)<='9') {
                year=year*10+str.charAt(i)-'0';
            }
        }
        int month=check1(month1);
        if(year<1582) {
            int startyear=1,startmonth=1,startday=1;
            while(true) {
                check2(startyear);
                if(startyear==year&&startmonth==month&&startday==day) {
                    break;
                }
                startday++;
                count++;
                if(startday>days[startmonth]) {
                    startday=1;
                    startmonth++;
                }
                if(startmonth>12) {
                    startmonth=1;
                    startyear++;
                }
            }
        }
        if(year==1582) {
            int startmonth=1,startday=1;
            count=577460;
            days[10]=21;
            while(true) {
                check2(year);
                if(startmonth==month&&startday==day) {
                    break;
                }
                startday++;
                count++;
                if(startday>days[startmonth]) {
                    startday=1;
                    startmonth++;
                }
                if(startmonth>12) {
                    startmonth=1;
                }
            }
        }
        if(year>1582) {
            int startyear=1583,startmonth=1,startday=1;
            count=577815;
            days[10]=31;
            while(true) {
                check2(startyear);
                if(startyear==year&&startmonth==month&&startday==day) {
                    break;
                }
                startday++;
                count++;
                if(startday>days[startmonth]) {
                    startday=1;
                    startmonth++;
                }
                if(startmonth>12) {
                    startmonth=1;
                    startyear++;
                }
            }
        }
        System.out.println(count);
    }
    public static int check1(String month) {
        switch(month) {
            case "JAN":return 1;
            case "FEB":return 2;
            case "MAR":return 3;
            case "APR":return 4;
            case "MAY":return 5;
            case "JUN":return 6;
            case "JUL":return 7;
            case "AUG":return 8;
            case "SEP":return 9;
            case "OCT":return 10;
            case "NOV":return 11;
            case "DEC":return 12;
        }
        return 0;
    }
    public static void check2(int year) {
        if(year<1582) {
            if(year%4==0) {
                days[2]=29;
            }
            else {
                days[2]=28;
            }
        }
        if(year>=1582) {
            if(year%400==0||(year%4==0&&year%100!=0)) {
                days[2]=29;
            }
            else {
                days[2]=28;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值