常见日期处理

#include <cstdio>

int months[2][13]={
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int isrun(int year){
    if (year %400==0 || (year%4==0 && year%100!=0))return 1;
    else return 0;
}
void addoneday(int &year,int &month,int &day){
    day++;
    if (day>months[isrun(year)][month]){
        month++;
        day = 1;
    }
    if (month>12){
        month=1;
        year++;
    }
}
void suboneday(int &year,int &month,int &day){
    day--;
    if (day==0){
        month--;
        //这里不应该直接对day进行赋值,应该月份确定下来再赋值,因为月份有可能也不够减
        //day = months[isrun(year)][month];
    }
    if (month==0){
        year--;
        month = 12;
    }
    if (day==0)day = months[isrun(year)][month];
}
// 计算所在年份的第几天

int cal_(int& year ,int &month,int &day){
    int cnt=0;
    while (month!=1||day!=1){
        suboneday(year,month,day);
        cnt++;
    }
    cnt++;
    return cnt;
}
int isbefore(int year_1,int year_2,int month_1,int month_2,int day_1,int day_2){
    if (year_1!=year_2) return year_1<year_2;
    if (month_1!=month_2)return month_1<month_2;
    return day_1<day_2;
}
int which_week(int year,int month,int day){
    // 可以从题目中的输入输出得到一天为周几
    int y = 2021;
    int m = 5;
    int d = 1;
    int dayofweek = 6;
    if (isbefore(year,y,month,m,day,d)){

        while (year!=y || month!= m||day !=d){
            suboneday(y,m,d);
            dayofweek--;
            if (dayofweek==-1)dayofweek = 6;
        }
    }else{
        while (year!=y || month!= m||day !=d){
            addoneday(y,m,d);
            dayofweek++;
            if (dayofweek==7)dayofweek = 0;
        }

    }
    return dayofweek;


}
int main(){
    /*
    int year,month,day;
    scanf("%d-%d-%d",&year,&month,&day);
    int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++)suboneday(year,month,day);

    printf("%4d-%02d-%02d",year,month,day);

    */

    int year,month,day;
    scanf("%d-%d-%d",&year,&month,&day);
    printf("%d",which_week(year,month,day));

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值