3.4日期处理

泪湿孤枕 提交于 2020-02-23 12:17:22

codeup 1928: 日期差值

题目描述

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

输入

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

输出

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

样例输入 Copy

20130101

20130105

样例输出 Copy

5

#include <stdio.h>
#include <iostream>
using namespace std;
int m[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}};
//每月天数,1维平年,2维闰年
bool isLeap(int y)                        //判断闰年函数
{
    return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}

int main()
{
    int y1, m1, d1, y2, m2, d2;         //第一,二个日期的年月日
    int p, q;                           //两个日期
    while (scanf("%d%d", &p, &q) != EOF)
    {
        if (p > q)                     //交换,使日期1较小
        {
            int temp = p;
            p = q;
            q = temp;
        }
                                    //计算第一,二个日期的年月日
        y1 = p / 10000;       
        m1 = (p / 100) % 100;
        d1 = p % 100;
        y2 = q / 10000;
        m2 = (q / 100) % 100;
        d2 = q % 100;
        int cnt = 1;              //日期差值
        while (y1 < y2 || m1 < m2 || d1 < d2)
        {
            d1++;
            cnt++;        
            if (d1 == m[m1][isLeap(y1)] + 1)      //当月天数满
            {
                d1 = 1;
                m1++;
            }
            if (m1 == 13)                        //当年月数满
            {
                m1 = 1;
                y1++;
            }
        }
        printf("%d\n", cnt);
    }

    return 0;
}

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.

样例输入 Copy

21 December 2012

5 January 2013

样例输出 Copy

Friday

Saturday

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int m[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[12][10] = {
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"};
char w[7][10] = {
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
    "Monday",
    "Tuesday"};        //取当天的星期几为w[0]; 例如今天2020/02/19,w[0]="Wednesday"
bool isLeap(int y)
{
    return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
int main()
{
    int d1;
    char str[10];
    int m1;
    int y1;
    int d2 = 19, m2 = 2, y2 = 2020; //取当前日期作为参照点
    while (scanf("%d %s %d", &d1, str, &y1) != EOF)
    {
        int cnt = 0;
        int sign = 0;
        for (int i = 0; i < 12; i++)
        {
            if (strcmp(str, month[i]) == 0)
            {
                m1 = i + 1;                  //将月份转换为整数
            }
        }
        if ((y1 >= y2) || (y1 >= y2 && m1 >= m2) || (y1 >= y2 && m1 >= m2 && d1 >= d2))  //假设当前日期比较大
        {
            sign = 1;                       //如果当前日期比较小,则交换,置sign为1
            int t1, t2, t3;
            t1 = y1, y1 = y2, y2 = t1;
            t2 = m1, m1 = m2, m2 = t2;
            t3 = d1, d1 = d2, d2 = t3;
        }
        while (y1 < y2 || m1 < m2 || d1 < d2)              //计算两个日期之间的差值,假设相邻两个日期之间的差值为1
        {
            d1++;
            cnt++;
            if (d1 == m[m1][(isLeap(y1))] + 1)
            {
                m1++;
                d1 = 1;
            }
            if (m1 == 13)
            {
                m1 = 1;
                y1++;
            }
        }
        if (sign == 0)                 //如果当前日期较大,将cnt=-cnt,累加7直至cnt>0(对7求余不变)
        {
            cnt = -cnt;
            while (cnt < 0)
            {
                cnt += 7;
            }
        } 
        printf("%s\n", w[cnt%7]);
    }                                                //1932 2 25
    return 0;
}

//公式计算 W = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D  Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。
//W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7  在公式中d表示日期中的日数,m表示月份数,y表示年数。
#include <cstdio>
#include <cstring>
 
char month[15][20] = { {}, {"January"}, {"February"}, {"March"}, {"April"}, {"May"}, 
        {"June"}, {"July"}, {"August"}, {"September"}, {"October"}, {"November"}, {"December"}
};
char week[15][20] = { {"Monday"}, {"Tuesday"}, {"Wednesday"}, {"Thursday"}, {"Friday"}, 
        {"Saturday"}, {"Sunday"}
};
 
int main()
{
    int d, y, k;
    char m[15];
    while (scanf ("%d %s %d", &d, m, &y) != EOF) {
        for (int i = 0; i < 13; i++) {
        if (strcmp (month[i], m) == 0)  k = i;
        }   
        if (k == 1 || k == 2) {                     //对1、2月进行修正; 
            k += 12;
            y--;
        }
        int j = (d + 2 * k + 3 * (k + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
        printf ("%s\n", week[j]);                   //0~6分别对应星期一到天; 
    }
 
    return 0;
}

打印日期

题目描述

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

输入

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

输出

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

样例输入 Copy

2013 60

2012 300

2011 350

2000 211

样例输出 Copy

2013-03-01

2012-10-26

2011-12-16

2000-07-29

#include <stdio.h>
#include <iostream>
using namespace std;
int m[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 y)
{
    return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
int main()
{
    int y, d;
    int n;
    while (scanf("%d %d", &y, &d) != EOF)
    {
        int month, day = 0;
        for (month = 0; day < d; month++)
        {
            day += m[month+1][(isLeap(y))];
        }
        day -= m[month][(isLeap(y))];
        for (n = 0; day < d; n++, day++)
            ;
        printf("%04d-%02d-%02d\n", y, month, n);
    }
    return 0;
}

日期类

题目描述

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

输入

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

输出

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

样例输入 Copy

2

1999 10 20

2001 1 31

样例输出 Copy

1999-10-21

2001-02-01

提示

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

#include <stdio.h>
#include <iostream>
using namespace std;
int mon[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 y)
{
    return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
int main()
{
    int m;
    scanf("%d", &m);
    int y1, m1, d1;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d %d", &y1, &m1, &d1);
        d1++;
        if (d1 == mon[m1][(isLeap(y1))] + 1)
        {
            d1 = 1;
            m1++;
        }
        if (m1 == 13)
        {
            y1++;
            m1 = 1;
        }
        printf("%04d-%02d-%02d\n", y1, m1, d1);
    }
    return 0;
}

日期累加

题目描述

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

输入

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

输出

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

样例输入 Copy

1

2008 2 3 100

样例输出 Copy

2008-05-13

#include <stdio.h>
#include <iostream>
using namespace std;
int mon[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 y)
{
    return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
int main()
{
    int m;
    scanf("%d", &m);
    int y1, m1, d1, ds;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d %d %d", &y1, &m1, &d1, &ds);
        while (ds--)
        {
            d1++;
            if (d1 == mon[m1][(isLeap(y1))] + 1)
            {
                d1 = 1;
                m1++;
            }
            if (m1 == 13)
            {
                y1++;
                m1 = 1;
            }
        }
        printf("%04d-%02d-%02d\n", y1, m1, d1);
    }
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!