HDU4545+计算日期

/*
计算过了D天后的日期
之前D天的日期
*/
#include<stdio.h>
int judge_year( int year ){
    if( (year%4==0&&year%100!=0)||(year%400==0) )
        return true;
    else
        return false;
}
int judge_month( int mon ){
    if( mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12 ) 
        return true;
    else
        return false;
}
int main(){
    int ca;
    //freopen("cmp.txt","w",stdout);
    scanf("%d",&ca);
    while( ca-- ){
        int n;
        scanf("%d",&n);

        int year,mon,day;
        year = 2013;
        mon = 3;
        day = 24;
        int sum = n;
        while( sum>0 ){
            if( judge_year(year)==true ){
               
                //printf("sum=%d,,year = %d,mon=%d,day=%d\n",sum,year,mon,day);
                    if( mon==2 ){
                        int left = 29-day;
                        if( sum>left ){
                            sum = sum-(left+1);
                            mon++;
                            day = 1;
                                        }
                        else if( sum==left ){
                            sum = 0;
                            day = 29;
                                    }
                        else {
                            day += sum;
                            sum = 0;
                        }    
                    }
                    else if( judge_month( mon )==true ){
                        int left = 31-day;
                        if( sum>left ){
                            sum = sum-left-1;
                            mon++;
                            if( mon>12 ) {
                                year++;
                                mon = 1;
                            }
                            day = 1;
                        }
                        else if( sum==left ){
                            sum = 0;
                            day = 31;
                        }
                        else{
                            day += sum;
                            sum = 0;
                        }
                    }
                    else {
                        int left = 30-day;
                        if( sum>left ){
                            sum = sum-left-1;
                            mon++;
                            if( mon>12 ) {
                                year++;
                                mon = 1;
                            }
                            day = 1;
                        }
                        else if( sum==left ){
                            sum = 0;
                            day = 30;
                        }
                        else{
                            day += sum;
                            sum = 0;
                        }
                    }
                
            }//闰年
            else{
                
                    if( mon==2 ){
                        int left = 28-day;
                        if( sum>left ){
                            sum = sum-left-1;
                            mon++;
                            day = 1;
                        }
                        else if( sum==left ){
                            sum = 0;
                            day = 28;
                        }
                        else {
                            day += sum;
                            sum  = 0;
                        }    
                    }
                    else if( judge_month( mon )==true ){
                        int left = 31-day;
                        if( sum>left ){
                            sum = sum-left-1;
                            mon++;
                            if( mon>12 ) {
                                year++;
                                mon = 1;
                            }
                            day = 1;
                        }
                        else if( sum==left ){
                            sum = 0;
                            day = 31;
                        }
                        else{
                            day += sum;
                            sum = 0;
                        }
                    }
                    else {
                        int left = 30-day;
                        if( sum>left ){
                            sum = sum-left-1;
                            mon++;
                            if( mon>12 ) {
                                mon = 1;
                                year++;
                            }
                            day = 1;
                        }
                        else if( sum==left ){
                            sum = 0;
                            day = 30;
                        }
                        else{
                            day += sum;
                            sum = 0;
                        }
                    }
                
            }//不是闰年
            if( sum<=0 ) 
                break;
        }//向后D天的日期

        printf("%04d/%02d/%02d ",year,mon,day);
        year = 2013;
        mon = 3;
        day = 24;
        sum = n;
        while( sum>0 ){
            if( judge_year( year )==true ){
                if( sum>=366 ){
                    sum-=366;
                    year--;
                }
                else{
                    if( mon==3 ){
                        int left = day;
                        if( sum>=29 ){
                            sum-=29;
                            mon--;
                        }
                        else {
                            if( sum==left-1 ){
                                sum = 0;
                                day = 1;
                            }
                            else if( sum<left-1 ){
                                day -= sum;
                                sum = 0;
                            }
                            else {
                                sum -= left;
                                day = 29;
                                mon--;
                            }
                        }
                    }
                    else if( mon==5||mon==7||mon==10||mon==12 ){
                        int left = day;
                        if( sum>=30 ){
                            sum-=30;
                            mon--;
                        }
                        else {
                            if( sum==left-1 ){
                                sum = 0;
                                day = 1;
                            }
                            else if( sum<left-1 ){
                                day -= sum;
                                sum = 0;
                            }
                            else {
                                sum -= left;
                                day = 30;
                                mon--;
                            }
                        }
                    }
                    else {
                        int left = day;
                        if( sum>=31 ){
                            sum-=31;
                            mon--;
                            if( mon<=0 ){
                                year--;
                                mon=12;
                            }
                        }
                        else {
                            if( sum==left-1 ){
                                sum = 0;
                                day = 1;
                            }
                            else if( sum<left-1 ){
                                day -= sum;
                                sum = 0;
                            }
                            else {
                                sum -= left;
                                day = 31;
                                mon--;
                                if( mon==0 ){
                                    mon = 12;
                                    year--;
                                }
                            }
                        }
                    }
                }
            }//闰年
            else {
                if( sum>=365 ){
                    sum-=365;
                    year--;
                }
                else{
                    if( mon==3 ){
                        int left = day;
                        if( sum>=28 ){
                            sum-=28;
                            mon--;
                        }
                        else {
                            if( sum==left-1 ){
                                sum = 0;
                                day = 1;
                            }
                            else if( sum<left-1 ){
                                day -= sum;
                                sum = 0;
                            }
                            else {
                                sum -= left;
                                day = 28;
                                mon--;
                            }
                        }
                    }
                    else if( mon==5||mon==7||mon==10||mon==12 ){
                        int left = day;
                        if( sum>=30 ){
                            sum-=30;
                            mon--;
                        }
                        else {
                            if( sum==left-1 ){
                                sum = 0;
                                day = 1;
                            }
                            else if( sum<left-1 ){
                                day -= sum;
                                sum = 0;
                            }
                            else {
                                sum -= left;
                                day = 30;
                                mon--;
                            }
                        }
                    }
                    else {
                        int left = day;
                        if( sum>=31 ){
                            sum-=31;
                            mon--;
                            if( mon==0 ){
                                year--;
                                mon=12;
                            }
                        }
                        else {
                            if( sum==left-1 ){
                                sum = 0;
                                day = 1;
                            }
                            else if( sum<left-1 ){
                                day -= sum;
                                sum = 0;
                            }
                            else {
                                sum -= left;
                                day = 31;
                                mon--;
                                if( mon==0 ){
                                    mon = 12;
                                    year--;
                                }
                            }
                        }
                    }
                }
            }//不是闰年
            if( sum<=0 ) break;
        }

        printf("%04d/%02d/%02d\n",year,mon,day);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值