《算法笔记》3.4小节

关于日期计算,不超时很重要。这里有些代码是可重用的。

问题A:

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。

输入

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出

每组数据输出一行,即日期差值

样例输入

20130101
20130105

样例输出

5

书上那个版本是超时的,但是便于理解

#include<cstdio>
int isleap(int year){
    if(year%400==0||(year%100!=0&&year%4==0))
    return 0;
    else
    return 1;
}
int main(){
    freopen("in.txt","r",stdin);
    int a[2][13]={{0,31,29,31,30,31,30,31,31,30,31,30,31},
                  {0,31,28,31,30,31,30,31,31,30,31,30,31}
    };
    int time1,time2;
    while(scanf("%d%d",&time1,&time2)!=-1){
        int y1,m1,d1,y2,m2,d2;
            if(time1>time2){
            int temp=time2;
            time2=time1;
            time1=temp;
        }//简直日了狗了,我居然写错了这段代码,导致改了无数次没找出错误
        y1=time1/10000;
        m1=time1%10000/100;
        d1=time1%100;
            y2=time2/10000;
        m2=time2%10000/100;
        d2=time2%100;
        int ans=1;
        if(y1<y2){
            ans+=(a[isleap(y1)][m1]-d1);
            m1++;
            if(m1==13){
                m1=1;
                y1++;
            }
            else{
                while(m1!=13){
                    ans+=(a[isleap(y1)][m1]);
                    m1++;
                }
                m1=1;
                y1++;
            }
            while(y1!=y2){
                if(isleap(y1)==0)
                ans+=366;
                else
                ans+=365;
                y1++;
            }
            while(m1<m2){
                ans+=a[isleap(y2)][m1];
                m1++;
            }
            ans+=d2;
        }
        else if(m1<m2){
            ans+=(a[isleap(y1)][m1]-d1);
            m1++;
                while(m1<m2){
                ans+=a[isleap(y2)][m1];
                m1++;
            }
            ans+=d2;
        }
        else if(d1<d2){
            ans+=(d2-d1);
        }
         printf("%d\n",ans);
    }
    return 0;
}

问题B:

题目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

样例输入

21 December 2012
5 January 2013

样例输出

Friday
Saturday
#include<cstdio>
#include<cstring>
int isleap(int year){
    if(year%400==0||(year%4==0&&year%100!=0))
    return 0;
    else
    return 1;
}
int main(){
//    freopen("in.txt","r",stdin);
    int a[2][13]={{0,31,29,31,30,31,30,31,31,30,31,30,31},
                  {0,31,28,31,30,31,30,31,31,30,31,30,31}
    };
    char* mo[13]={"0","January","February","March","April","May","June"
    ,"July","August","September","October","November","December"
    };
    char* da[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int day,year;
    char month[20];
    while(scanf("%d%s%d",&day,&month,&year)!=-1){
        int y1,m1,d1,y2,m2,d2,i;
        for(i=1;i<13;i++){
            if(!strcmp(month,mo[i]))
            m2=i;
        }
        int flag=0;
    int     time2=year*10000+m2*100+day;
    int     time1=20121221;
    //    printf("%d\n",time2);
        if(time1>time2){
            int temp=time2;
            time2=time1;
            time1=temp;
            flag=1;
        }
        y1=time1/10000;
        m1=time1%10000/100;
        d1=time1%100;
            y2=time2/10000;
        m2=time2%10000/100;
        d2=time2%100;
        int ans=0;
        if(y1<y2){
            ans+=(a[isleap(y1)][m1]-d1);
            m1++;
            if(m1==13){
                m1=1;
                y1++;
            }
            else{
                while(m1!=13){
                    ans+=(a[isleap(y1)][m1]);
                    m1++;
                }
                m1=1;
                y1++;
            }
            while(y1!=y2){
                if(isleap(y1)==0)
                ans+=366;
                else
                ans+=365;
                y1++;
            }
            while(m1<m2){
                ans+=a[isleap(y2)][m1];
                m1++;
            }
            ans+=d2;
        }
        else if(m1<m2){
            ans+=(a[isleap(y1)][m1]-d1);
            m1++;
                while(m1<m2){
                ans+=a[isleap(y2)][m1];
                m1++;
            }
            ans+=d2;
        }
        else if(d1<d2){
            ans+=(d2-d1);
        }
        int d;
        if(flag==0){
            d=(5+ans%7)%7;
        }
        else{
            d=5-ans%7;
            //    printf("%d\n",d);
            if(d==-1){
                d=6;
            }
        //    printf("%d\n",d);
        }
        printf("%s\n",da[d]);
    }
    return 0;
}
问题C:

题目描述

给出年分m和一年中的第n天,算出第n天是几月几号。

输入

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出

可能有多组测试数据,对于每组数据,按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

样例输入

2013 60
2012 300
2011 350
2000 211

样例输出

2013-03-01
2012-10-26
2011-12-16
2000-07-29
#include<cstdio>
int isleap(int year){
    if(year%400==0||(year%100!=0&&year%4==0))
    return 0;
    else
    return 1;
}
int main(){
//    freopen("in.txt","r",stdin);
    int a[2][13]={{0,31,29,31,30,31,30,31,31,30,31,30,31},
                  {0,31,28,31,30,31,30,31,31,30,31,30,31}
    };
    int year,n;
    while(scanf("%d%d",&year,&n)!=-1){
    int month=1,day=0;
    for(int i=0;i<n;i++){
    //    printf("%d\n",day);
        day++;
        if(day==a[isleap(year)][month]+1){
            day=1;
            month++;
        }
    }//没什么难度,关键是输出格式
    if(year<1000&&year>=100)
    printf("0");
    if(year>=10&&year<100)
    printf("00");
    if(year>=1&&year<10)
    printf("000");
    if(month<10&&day<10)
    printf("%d-0%d-0%d\n",year,month,day);    
    if(month<10&&day>=10)
    printf("%d-0%d-%d\n",year,month,day);    
    if(month>=10&&day<10)
    printf("%d-%d-0%d\n",year,month,day);    
    if(month>=10&&day>=10)
    printf("%d-%d-%d\n",year,month,day);    
    }
    return 0;
}


问题D:

题目描述

编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作

输入

输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。

输出

输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。

样例输入

2
1999 10 20
2001 1 31

样例输出

1999-10-21
2001-02-01
#include<cstdio>
int main(){
    freopen("in.txt","r",stdin);
    int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int m;
    scanf("%d",&m);
    while(m--){
            int year,month,day;
    scanf("%d%d%d",&year,&month,&day);
        day++;
        if(day==a[month]+1){
            day=1;
            month++;}
    if(year<1000&&year>=100)
    printf("0");
    if(year>=10&&year<100)
    printf("00");
    if(year>=1&&year<10)
    printf("000");
    if(month<10&&day<10)
    printf("%d-0%d-0%d\n",year,month,day);    
    if(month<10&&day>=10)
    printf("%d-0%d-%d\n",year,month,day);    
    if(month>=10&&day<10)
    printf("%d-%d-0%d\n",year,month,day);    
    if(month>=10&&day>=10)
    printf("%d-%d-%d\n",year,month,day);    
    }
    return 0;
}
问题E:

题目描述

设计一个程序能计算一个日期加上若干天后是什么日期。

输入

输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。

输出

输出m行,每行按yyyy-mm-dd的个数输出。

样例输入

1
2008 2 3 100

样例输出

2008-05-13

#include<cstdio>
int isleap(int year){
    if(year%400==0||(year%100!=0&&year%4==0))
    return 0;
    else
    return 1;
}
int main(){
    freopen("in.txt","r",stdin);
    int a[2][13]={{0,31,29,31,30,31,30,31,31,30,31,30,31},
                  {0,31,28,31,30,31,30,31,31,30,31,30,31}
    };
    int m;
    scanf("%d",&m);
    while(m--){
        int year,month,day,n;
    scanf("%d%d%d%d",&year,&month,&day,&n);
    for(int i=0;i<n;i++){
        day++;
        if(day==a[isleap(year)][month]+1){
            day=1;
            month++;
        }
        if(month==13){
            year++;
            month=1;
        }
    }
    if(year<1000&&year>=100)
    printf("0");
    if(year>=10&&year<100)
    printf("00");
    if(year>=1&&year<10)
    printf("000");
    if(month<10&&day<10)
    printf("%d-0%d-0%d\n",year,month,day);    
    if(month<10&&day>=10)
    printf("%d-0%d-%d\n",year,month,day);    
    if(month>=10&&day<10)
    printf("%d-%d-0%d\n",year,month,day);    
    if(month>=10&&day>=10)
    printf("%d-%d-%d\n",year,month,day);    
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值