ACM 2018第十届四川省大学生程序设计竞赛省赛 E题 Ever17

题目训练网址

E题训练网址: https://www.oj.swust.edu.cn/problem/show/2808
第十届省赛所有赛题网址: https://www.oj.swust.edu.cn/contest/show/1253

题意翻译

日期有两种格式 “MM/DD/YY”(月/日/年) 或者 “YY/MM/DD”(年/月/日)。现在输入这种格式的日期。如果只有一个可能的日期可以使用“MM / DD / YY”或“YY / MM / DD”格式,则以“月份日期,年份”的格式输出日期(如"19/02/07"输出"February 7, 2019");否则,输出两个可能的不同日期之间距离的天数。

AC代码以及注释

为了容易看懂,我把每个小功能都单独写成一个函数。
要注意两种格式都合法,并且两种格式最后指定的日期都是同一天的话(如“01/01/01”),要按格式输出日期,而不是0。

#include <iostream>
#include <string>
using namespace std;

int monthdays[13]={-1,31,29,31,30,31,30,31,31,30,31,30,31};
string monthstr[13]={" ","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"};
int a,b,c;
void toint(string str){//将xx/xx/xx格式的字符串中xx的值分别赋值给全局变量a,b,c
    a=str[1]-'0'+10*(str[0]-'0');
    b=str[4]-'0'+10*(str[3]-'0');
    c=str[7]-'0'+10*(str[6]-'0');
}
bool txtyes(int Y,int M,int D){//判断日期的格式是否正确 参数输入格式 年-月-日
    if(Y%4==0)
        monthdays[2]=29;
    else
        monthdays[2]=28;
    if(M>12||M<1)
        return false;
    else if(D>monthdays[M]||D<1)
        return false;
    else
        return true;
}
bool lr(int Y,int M,int D,int year,int month,int date){//判断左边的日期是否大于等于右边的日期
    if(Y<year)
        return false;
    if(Y>year)
        return true;
    if(M<month)
        return false;
    if(M>month)
        return true;
    if(D<date)
        return false;
    if(D>=date)
        return true;

}
int getdays(int Y,int M,int D,int year,int month,int date){//计算日期Y-M-D与year-month-date之间距离多少天
    int n1,n2,n3;
    if(lr(Y,M,D,year,month,date)){//确保Y-M-D日期在year-month-date之前,或者相等
        n1=Y;n2=M;n3=D;
        Y=year;M=month;D=date;
        year=n1;month=n2;date=n3;
    }
    int days=0; //记录天数
    for(int x=Y;x<year;x++){
        if(x%4==0&&x%100!=0||x%400==0)
            days+=366;
        else
            days+=365;
    }
    if(year%4==0&&year%100!=0||year%400==0)
        monthdays[2]=29;
    else
        monthdays[2]=28;

    for(int m=1;m<month;m++)
        days+=monthdays[m];
    days+=date;
    //减去Y年的天数
    if(Y%4==0&&Y%100!=0||Y%400==0)
        monthdays[2]=29;
    else
        monthdays[2]=28;
    for(int m=1;m<M;m++)
        days-=monthdays[m];
    days-=D;
    return days;
}
void totxtyes(int e,int f,int g){//确保a-b-c日期格式正确
    if(!txtyes(e,f,g)){
        a=g;b=e;c=f;
    }
    return;
}
void run(){//调用函数,实现功能,方便使用main()调试其他函数。
    int n;
    string sc;
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> sc;
        toint(sc);
        if(txtyes(a,b,c)&&txtyes(c,a,b)&&getdays(a,b,c,b,c,a)!=0){
            cout <<getdays(a,b,c,c,a,b)<< endl;
        }else{
            totxtyes(a,b,c);
            cout << monthstr[b] << " " << c<<", "<<2000+a<<endl;
        }

    }
}

int main(){
    run();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值