Contest100000578 - 《算法笔记》3.4小节——入门模拟->日期处理

1928 Problem A 日期差值

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

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

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

样例输入

20130101
20130105

样例输出

5

#include <cstdio> 
int data[13][2] = {
    {0,0},
    {31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},
    {31,31},{30,30},{31,31},{30,30},{31,31}
};
bool leap(int year){
    return (year%4==0 && year%100!=0)||(year%400==0);
}
int main() {
    int time1,y1,m1,d1,time2,y2,m2,d2;
    while(scanf("%d%d", &time1, &time2)!=EOF){
        if(time1>time2){
            int temp=time1;
            time1=time2;
            time2=temp;
        }
        y1 = time1 / 10000 ,m1 = time1 % 10000 / 100 ,d1 = time1 % 100;
        y2 = time2 / 10000 ,m2 = time2 % 10000 / 100 ,d2 = time2 % 100;
        int ans=1;
        while(y1<y2||m1<m2||d1<d2){
            d1++;
            if(d1==data[m1][leap(y1)]+1){
                m1++;
                d1=1;
            }
            if(m1==13){
                y1++;
                m1=1;
            }
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

1929 Problem B Day of Week

题目描述
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 month[13][2]={
    {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},
    {30,30},{31,31},{30,30},{31,31}
};
char month_name[13][20]={
    "","January","February","March","April","May","June","July","August",
    "September","October","November","December"
};
char week_name[7][20]={
    "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
};
bool isLeap(int year){
    return (year%4==0&&year%100!=0)||(year%400==0);
}
int main(){
    int d,m,y;
    char s[20];
    while(scanf("%d%s%d",&d,s,&y)!=EOF){
        for(m=1;m<=12;m++){
            if(strcmp(s,month_name[m])==0) break;
        }
        int y1=1,m1=1,d1=1,day=1;
        while(y1<y||m1<m||d1<d){
            d1++;
            if(d1==month[m1][isLeap(y1)]+1){
                d1=1;
                m1++;
            }
            if(m1==13){
                m1=1;
                y1++;
            }
            day++;
        }
        printf("%s\n",week_name[day%7]);
    }
    return 0;
}

1931 Problem 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 month[13][2]={
    {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},
    {30,30},{31,31},{30,30},{31,31}
};
bool isLeap(int year){
    return (year%4==0&&year%100!=0)||(year%400==0);
}
int main(){
    int y,n;
    while(scanf("%d%d",&y,&n)!=EOF){
        int m=1,d=1;
        while(n!=1){
            d++;
            if(d==month[m][isLeap(y)]+1){
                d=1;
                m++;
            }
            n--;
        }
        printf("%04d-%02d-%02d\n",y,m,d);
    }
    return 0;
}

2026 Problem D 日期类

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

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

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

样例输入

2
1999 10 20
2001 1 31

样例输出

1999-10-21
2001-02-01

提示
注意个位数日期前面要有0。

#include <cstdio>
int month[13]={
    0,31,28,31,30,31,30,31,31,30,31,30,31
};
int main(){
    int n,y,m,d;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d%d",&y,&m,&d);
        d++;
        if(d==month[m]+1){
            d=1;
            m++;
        }
        if(m==13){
            m=1;
            y++;
        }
        printf("%04d-%02d-%02d\n",y,m,d);
    }
    return 0;
}

2063 Problem E 日期累加

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

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

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

样例输入

1
2008 2 3 100

样例输出

2008-05-13

#include <cstdio>
int month[13][2]={
    {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},
    {30,30},{31,31},{30,30},{31,31}
};
bool isLeap(int year){
    return (year%4==0&&year%100!=0)||(year%400==0);
}
int main(){
    int n,y,m,d,ans;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d%d%d",&y,&m,&d,&ans);
        for(int i=ans;i>0;i--){
            d++;
            if(d==month[m][isLeap(y)]+1){
                m++;
                d=1;
            }
            if(m==13){
                y++;
                m=1;
            }
        }
        printf("%04d-%02d-%02d\n",y,m,d);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c1014yzh/article/details/87913422