算法笔记 之日期处理

1.4、日期处理

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

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

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

样例输入
20130101
20130105
样例输出
5

#include<bits/stdc++.h>
using namespace std;
bool isLeap(int year)
{
    if(year%4==0&&year%100!=0||year%400==0)
    {
        return true;
    }
    else
        return false;
}
int main()
{
    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}};
    int time1,y1,m1,d1;
    int 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==month[m1][isLeap(y1)]+1)
            {
                m1++;
                d1=1;
            }
            if(m1==13)
            {
                y1++;
                m1=1;
            }
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


问题 B: Day of Week
时间限制: 1 Sec  内存限制: 32 MB
提交: 2192  解决: 675
[提交][状态][讨论版][命题人:外部导入]
题目描述
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<bits/stdc++.h>
using namespace std;
bool isLeap(int year)
{
    if(year%4==0&&year%100!=0||year%400==0)
    {
        return true;
    }
    else
        return false;
}
int main()
{
    int d,y,sum,k,i;
    char m[20];
    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 a[15][20] = {"0","January","February","March","April","May","June","July","August","September","October","November","December"};
    char b[10][20] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    while(scanf("%d %s %d",&d,m,&y)!=EOF)
    {
        sum=0;
        for(i=1;i<y;i++)
        {
            if(isLeap(i))
            sum+=366;
            else
            sum+=365;
        }
        for(k=1;k<13;k++)
        {
            if(strcmp(m,a[k])==0)
                break;
        }
        for(i=1;i<k;i++)
        {
            sum+=month[i][isLeap(y)];
        }
        sum+=d;
        if(sum%7==0)
        printf("Sunday\n");
        else
        printf("%s\n",b[sum%7-1]);
    }
    return 0;
}

问题 C: 打印日期
时间限制: 1 Sec  内存限制: 32 MB
提交: 2060  解决: 713
[提交][状态][讨论版][命题人:外部导入]
题目描述
给出年分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<bits/stdc++.h>
using namespace std;
bool isLeap(int year)
{
    if(year%4==0&&year%100!=0||year%400==0)
    {
        return true;
    }
    else
        return false;
}
int main()
{
    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}};
    int y,n;
    int m,d;
    while(~scanf("%d %d",&y,&n))
    {
       d=0;
       m=1;
       while(n>0)
       {
           d++;
           if(d==month[m][isLeap(y)]+1)
           {
               m++;
               d=1;
           }
           if(m==13)
           {
               y++;
               m=1;
           }
           n--;
       }
       printf("%04d-%02d-%02d\n",y,m,d);
    }
    return 0;
}

问题 D: 日期类
时间限制: 1 Sec  内存限制: 32 MB
提交: 828  解决: 602
[提交][状态][讨论版][命题人:外部导入]
题目描述
编写一个日期类,要求按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<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        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}};
        int y,m,d;
        cin>>y>>m>>d;
        d=d+1;
        if(d>month[m][0])
        {
            d=1;
            m++;
        }
        if(m==13)
        {
            y++;
            m=1;
        }
        printf("%04d-%02d-%02d\n",y,m,d);
    }
    return 0;
}

问题 E: 日期累加
时间限制: 1 Sec  内存限制: 32 MB
提交: 1173  解决: 625
[提交][状态][讨论版][命题人:外部导入]
题目描述
设计一个程序能计算一个日期加上若干天后是什么日期。

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

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

样例输入
1
2008 2 3 100
样例输出
2008-05-13

#include<bits/stdc++.h>
using namespace std;
bool isLeap(int year)
{
    if(year%4==0&&year%100!=0||year%400==0)
    {
        return true;
    }
    else
        return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        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}};
        int y,m,d,s;
        cin>>y>>m>>d>>s;
        while(s>0)
        {
            d++;
            if(d==month[m][isLeap(y)]+1)
            {
                m++;
                d=1;
            }
            if(m==13)
            {
                y++;
                m=1;
            }
            s--;
        }
        printf("%04d-%02d-%02d\n",y,m,d);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值