洛谷 T156530 儒略历(传智杯-练习赛)

T156530 儒略历

题目描述

在 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 日,中间的日期就当作不存在了。

img

现在给出日期,计算这个日期到公元 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

输出格式

输出一个整数表示答案

输入输出样例

输入 #1

1JAN1

输出 #1

0

输入 #2

4OCT1582

输出 #2

577736

输入 #3

15OCT1582

输出 #3

577737

输入 #4

21NOV2020

输出 #4

737751

AC代码

#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int MONTH[2][13]={
{0,
31,28,31,30,31,
30,31,31,30,31,
30,31},
{1,
31,29,31,30,31,
30,31,31,30,31,
30,31}
};
int monIndex(char s[]){
    if(strcmp(s, "JAN")==0) return 1; 
    if(strcmp(s, "FEB")==0) return 2; 
    if(strcmp(s, "MAR")==0) return 3; 
    if(strcmp(s, "APR")==0) return 4; 
    if(strcmp(s, "MAY")==0) return 5; 
    if(strcmp(s, "JUN")==0) return 6; 
    if(strcmp(s, "JUL")==0) return 7; 
    if(strcmp(s, "AUG")==0) return 8; 
    if(strcmp(s, "SEP")==0) return 9; 
    if(strcmp(s, "OCT")==0) return 10; 
    if(strcmp(s, "NOV")==0) return 11; 
    if(strcmp(s, "DEC")==0) return 12; 
    return 0;
}
int strToInt (char s[],int start, int end){
    int num=0;

    for(int i=start;i<end;i++){
        num+=(int)pow(10,end-i-1)*(s[i]-'0');
    }
    return num;
}
bool isRUN (int n){return n%4==0;}
bool isRUN2 (int n){return n%400==0 || (n%4==0 && n%100!=0);}
bool isNum(char n){return (n>='0'&&n<='9');}
void setDatas(char s[], int& day, int& mth, int& year){
    int flag=0, len=strlen(s);
    char str[4];

    for(int i=0;i<len;i++){
        if(flag==0&&!isNum(s[i])){
            day = strToInt(s,0,i);
            flag=1;
        }
        if(flag==1&&isNum(s[i])){
            str[0]=s[i-3];
            str[1]=s[i-2];
            str[2]=s[i-1];
            str[3]='\0';
            mth=monIndex(str);
            flag=2;
        }
        if(flag==2){
            year = strToInt(s,i,len);
            break;
        }
    }
}
int main()
{    
    char s[100];
    cin>>s;

    int day,mth,year;
    setDatas(s,day,mth,year);    //根据字符串 s 设置年月日
    
    // cout<<"day = "<<day<<endl; 
    // cout<<"mth = "<<mth<<endl; 
    // cout<<"year = "<<year<<endl; 
    int ans =0;

    if (year <= 1582) {
        //把年加进去
        for(int i=1;i<year;i++){
            if(isRUN(i)) ans+=366;
            else ans+=365;
        }
        //把月加进去
        for(int i=1;i<mth;i++){
            ans+=MONTH[isRUN(year)][i];
        }
        //1582年份的特殊判断
		if (year == 1582&&mth>=10&&day>4) {
            if(mth==10&&day<=15) ans-=day-5;
            else ans-=10;
		}
        //把天加进去
        ans+=(day-1);
	} else {
        //把年加进去
        for(int i=1;i<=1582;i++){
            if(isRUN(i)) ans+=366;
            else ans+=365;
        }
		for(int i=1+1582;i<year;i++){
            if(isRUN2(i)) ans+=366;
            else ans+=365;
        }
        //把月加进去
        for(int i=1;i<mth;i++){
            ans+=MONTH[isRUN2(year)][i];
        }
		//把天加进去,注意:10(十天是因为1582年10月的5-14日不存在)
        ans+=(day-10-1);
	}
    cout<<ans<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值