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

@[toc]

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

1928-ProblemA-日期差值

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

题析:经典的日期模拟,注意平年闰年的差别,两个时间点之间的差值用较小的时间不断累加来追上较大的时间,累加时注意到达界限时上一个级别的更新。

//1928ProblemA日期差值 
#include 
#include 
#include 
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 monthDay[month1][isLeap(year1)])//天数满月加一
            {
                month1++;
                day1=1;     
            }           
            if(month1 > 12)//月数满年加一
            {
                year1++;
                month1=1;
            }
            count++;
        }
        count=count+1;//如果两个日期是连续的我们规定他们之间的天数为两天
        printf("%d\n",count);
    }
    return 0;
}


1929-ProblemB-Day of Week

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

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


//1929ProblemBDay of Week
#include 
#include 
#include 
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;
}


1931-ProblemC-打印日期

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

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

//1931ProblemC打印日期 
#include 
#include 
#include 
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

2026-ProblemD-日期类

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

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

//2026ProblemD日期类 
#include 
#include 
#include 
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;
}


2063-ProblemE-日期累加

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


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

//2063ProblemE日期累加
#include 
#include 
#include 
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;
}


你可能感兴趣的:(《算法笔记》3.4小节——入门模拟->日期处理)