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

发布于 2020-01-28  190 次阅读


http://codeup.cn/contest.php?cid=100000578

Problem A: 日期差值

Time Limit: 1.000 Sec Memory Limit: 32 MB
Submit: 10889 Solved: 2470
[Submit] [Status] [Creator:Imported]
Description
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。

Input
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
Output
每组数据输出一行,即日期差值
Sample Input

20130101
20130105

Sample Output
5

代码(C++)

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

int monthDay[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)
{
    if((year%4==0&&year%100!=0) || year%400==0)
    {
        return true;
    }
    return false;
}
int main()
{
    int time1,year1,month1,day1;
    int time2,year2,month2,day2;
    while(scanf("%d%d",&time1, &time2) != EOF)
    {
        if(time1>time2)
        {
            int temp=time1;
            time1=time2;
            time2=temp;
        }
        year1 = time1/10000;
        month1 = time1%10000/100;
        day1 = time1%100;

        year2 = time2/10000;
        month2 = time2%10000/100;
        day2 = time2%100;
        int count=0;//计数值 
        while(year1<year2 || month1<month2 || day1<day2)//较小的时间点累加追较大的时间点
        {
            day1++;
            if(day1 > monthDay[month1][isLeap(year1)])//天数满月加一
            {
                month1++;
                day1=1;     
            }           
            if(month1 > 12)//月数满年加一
            {
                year1++;
                month1=1;
            }
            count++;
        }
        count=count+1;//如果两个日期是连续的我们规定他们之间的天数为两天
        printf("%d\n",count);
    }
    return 0;
}

Problem B: Day of Week

Time Limit: 1.000 Sec Memory Limit: 32 MB
Submit: 2311 Solved: 718
Description
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.

Input
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
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.

Sample Input Copy
21 December 2012
5 January 2013
Sample Output Copy
Friday
Saturday

注意:主要涉及基姆拉尔森计算公式,另外还需要注意:
字符数组的初始化问题
字符串比较函数strcmp==0时表示相等

代码(C++)

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
char monthName[13][20] = {{},
    {"January"},{"February"},{"March"},{"April"},{"May"},
    {"June"},{"July"},{"August"},{"September"},{"October"},
    {"November"},{"December"}
};//月份 

char weekday[7][20]={{"Monday"},{"Tuesday"},{"Wednesday"},{"Thursday"},{"Friday"},
                    {"Saturday"},{"Sunday"}
                };//星期 
/*
//基姆拉尔森计算公式外文名是Kim larsson calculation formula。
// W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7 //C++计算公式
在公式中d表示日期中的日数,m表示月份数,y表示年数。
注意:在公式中有个与其他公式不同的地方:
把一月和二月看成是上一年的十三月和十四月,
例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
*/
int main()
{
    int W,d,m,y;
    int day;
    char month[15];
    int year;
    while(scanf("%d%s%d",&day, month, &year) != EOF)
    {
        d=day;
        y=year;
        for(int i=1;i<=12;i++)
        {
            if(strcmp(monthName[i],month)==0)//strcmp字符串相等时值为0 
            {
                m=i;
            }
        }
        if(m==1 || m==2)//注意此处 把一月和二月看成是上一年的十三月和十四月,
        {
            m+=12;
            y--;
        }
        W=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
        printf("%s\n",weekday[W]);
    }
    return 0;
}

Problem C: 打印日期

Time Limit: 1.000 Sec Memory Limit: 32 MB
Submit: 2239 Solved: 768
Description
给出年分m和一年中的第n天,算出第n天是几月几号。

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

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

Sample Input Copy

2013 60
2012 300
2011 350
2000 211

Sample Output

2013-03-01
2012-10-26
2011-12-16
2000-07-29

注意:日期问题的变形,需注意:
Num--与day++的同步,此处不会出现year++的情况
打印日期的格式,需scanf的格式化输出
但法一为什么50%错误率还需进一步探讨

代码(C++)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int monthDay[13][10]={
    {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 year;
    int num;
    while(scanf("%d%d",&year, &num) != EOF)//输入参数 
    {
        int day=0;//天数计数 
        int month=1;//月份计数 
        //法二
        while(num>0)
        {
            num--;
            day++;
            if(day > monthDay[month][isLeap(year)])
            {
                day=1;
                month++; 
            }
        }
        printf("%04d-%02d-%02d\n",year,month,day);
        /*
        //法一 
        for(i=1;i<12;i++)
        {
            if(day <= num)
                day += monthDay[i][isLeap(year)];
            else
                break;
        }
        if(day<num)
        {
            day=num-day;
            printf("%04d-%02d-%02d\n",year,12,day);
        }
        else
        {
            i--;
            day=num + monthDay[i][isLeap(year)]-day;
            //打印输出年月日 
            printf("%04d-%02d-%02d\n",year,i,day);
        }
        */
    }
    return 0;
}

Problem D: 日期类

Time Limit: 1.000 Sec Memory Limit: 32 MB
Submit: 874 Solved: 639
Description
编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。

Input
输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。
Output
输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。
Sample Input

2
1999 10 20
2001 1 31

Sample Output

1999-10-21
2001-02-01

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

注意:简单的日期类问题,需注意:
多点测试n--
输出格式%04%02%02

代码(C++)

//2026ProblemD日期类 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int monthDay[13][10]={
            {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;
    scanf("%d",&n);
    int year,month,day;
    while(n--)
    {
        scanf("%d%d%d",&year, &month,&day);
        day++;
        if(day>monthDay[month][isLeap(year)])
        {
            day=1;
            month++;
        }
        if(month>12)
        {
            month=1;
            year++;
        }
        printf("%04d-%02d-%02d\n",year,month,day);
    }
    return 0;
}

Problem E: 日期累加

Time Limit: 1.000 Sec Memory Limit: 32 MB
Submit: 1262 Solved: 673
Description
设计一个程序能计算一个日期加上若干天后是什么日期。

Input
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
Output
输出m行,每行按yyyy-mm-dd的个数输出。
Sample Input

1
2008 2 3 100

Sample Output
2008-05-13

注意:
多点测试,n--
Num--与day++(涉及进位month++和year++)同步

代码(C++)

//2063ProblemE日期累加
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int monthDay[13][10]={{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 year,month,day,num;
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d%d",&year,&month,&day,&num);
        while(num--)
        {
            day++;
            if(day>monthDay[month][isLeap(year)])
            {
                day=1;
                month++;
            }
            if(month>12)
            {
                month=1;
                year++;
            }
        }
        printf("%04d-%02d-%02d\n",year,month,day);
    }
    return 0;
}

鸣谢

李霁明(https://blog.csdn.net/qq_34767784/article/details/88966745)

本文标题:《Contest100000578 – 《算法笔记》3.4小节——入门模拟->日期处理》

本文链接:https://wnag.com.cn/265.html

特别声明:除特别标注,本站文章均为原创,本站文章原则上禁止转载,如确实要转载,请电联:wangyeuuu@qq.com,尊重他人劳动成果,谢过~


正因为有要好好实现的梦想,所以今天也要好好加油。