hdu 1308 What Day Is It?

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1308

题目内容:

What Day Is It?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 832    Accepted Submission(s): 342


Problem Description
The calendar now in use evolved from the Romans. Julius Caesar codified a calendar system that came to be known as the Julian calendar. In this system, all months have 31 days, except for April, June, September, and November, which have 30 days, and February, which has 28 days in non-leap years, and 29 days in leap years. Also, in this system, leap years happened every four years. That is because the astronomers of ancient Rome computed the year to be 365.25 days long, so that after every four years, one needed to add an extra day to keep the calendar on track with the seasons. To do this, they added an extra day (February 29) to every year that was a multiple of four. 


Julian Rule: 

Every year that is a multiple of 4 is a leap year, i.e. has an extra day (February 29). 

In 1582, Pope Gregory's astronomers noticed that the year was not 365.25 days long, but closer to 365.2425. Therefore, the leap year rule would be revised to the following: 



Gregorian Rule: 

Every year that is a multiple of 4 is a leap year, unless it is a multiple of 100 that is not a multiple of 400. 

To compensate for how the seasons had shifted against the calendar up until that time, the calendar was actually shifted 10 days: the day following October 4, 1582 was declared to be October 15. 


England and its empire (including the United States) didn't switch to the Gregorian calendar system until 1752, when the day following September 2 was declared to be September 14. (The delay was caused by the poor relationship between Henry VIII and the Pope.) 


Write a program that converts dates in the United States using a calendar of the time and outputs weekdays. 
 

Input
The input will be a series of positive integers greater than zero, three integers per line, which represent dates, one date per line. The format for a date is ``month day year" where month is a number between 1 (which indicates January) and 12 (which indicates December), day is a number between 1 and 31, and year is positive number. 
 

Output
The output will be the input date and name of the weekday on which the given date falls in the format shown in the sample. An invalid date or nonexistent date for the calendar used in the United States at the time should generate an error message indicating a invalid date. The input will end with three zeroes
 

Sample Input
  
  
11 15 1997 1 1 2000 7 4 1998 2 11 1732 9 2 1752 9 14 1752 4 33 1997 0 0 0
 

Sample Output
  
  
November 15, 1997 is a Saturday January 1, 2000 is a Saturday July 4, 1998 is a Saturday February 11, 1732 is a Friday September 2, 1752 is a Wednesday September 14, 1752 is a Thursday 4/33/1997 is an invalid date.

题意:

万年历

题解:

这里我是把两端日期分为start 和 rear,start 是我们设的一个一直是星期几的日期(可以设为今天的时间),而rear就是要求是星期几的日期。然后按照时间用start 日期 一步步走到 rear日期,同时记录星期的变化。
万年历规则:
1、1582.10.4 下一天是 1582.10.15 , [5,14]不存在
2、1752.09.2 下一天是 1752.09.14 , [3,13]不存在
3、闰年规则:1582年之前的年份能被4整除就是闰年;1582年之后的年份能被4整除且不能被100整除  
或   能被400整除 就是闰年。

我这里分别用的是两种方式求星期几
1、一天一天的走
2、批量走:批量走我尽量分为三段来走:(1)start 一端的不足一年的时间(2)start到rear整年整年的时间(3)rear一端不足一年的时间


代码1:

#include<stdio.h>
int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//begin with 1
int leap_month_day[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};//begin with 1
typedef struct date
{
    int year;
    int month;
    int day;
    int week;
}date,* date_link;
char week_map[8][10]={"","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};//begin with 1
char month_map[13][10]={"","January","February","March","April","May","June","July","August","September","October","November","December"};//begin with 1
int in_year,in_month,in_day;
date date_start;
bool is_leap(int year)
{
    if(year<=1582) return(year%4==0);
    else return((year%4==0&&year%100!=0)||(year%400==0));
}
int comp_date(date a,date b)
{
    int a_num=a.year*10000+a.month*100+a.day;//ip to number to compare ip easily
    int b_num=b.year*10000+b.month*100+b.day;
    return(a_num-b_num);
}
void walk_day(date &a,int growth)
{
    //week
    a.week+=growth;
    if(a.week==0) a.week=7;
    if(a.week==8) a.week=1;
    //date
    if((a.year==1582&&a.month==10&&a.day==4&&growth==1)) growth=11;
    if((a.year==1582&&a.month==10&&a.day==15&&growth==-1)) growth=-11;
    if((a.year==1752&&a.month==9&&a.day==2&&growth==1)) growth=12;
    if((a.year==1752&&a.month==9&&a.day==14&&growth==-1)) growth=-12;
    a.day+=growth;
    if(a.day<1)
    {
        a.month--;
        a.day=is_leap(a.year)?leap_month_day[a.month]:month_day[a.month];
        if(a.month<=0)
        {
            a.year--;
            a.month=12;
            a.day=is_leap(a.year)?leap_month_day[a.month]:month_day[a.month];
        }
    }
    int bound = is_leap(a.year)?leap_month_day[a.month]:month_day[a.month];
    if(a.day>bound)
    {
        a.month++;
        a.day=1;
        if(a.month>=13)
        {
            a.year++;
            a.month=1;
            a.day=1;
        }
    }
}
int start_walk_rear(date &start,date &rear,int cmp)
{
    int growth=0;
    if(cmp>0) growth=-1;
    if(cmp<0) growth=1;
    //walk to year's start or end
    while(comp_date(start,rear)) walk_day(start,growth);
    rear.week=start.week;
    return(0);
}
bool is_valid_date(date a)
{
    if(a.year<=0) return(false);
    if(a.month<=0||a.month>=13) return(false);
    if(!is_leap(a.year)&&(a.day<=0||a.day>month_day[a.month])) return(false);
    if(is_leap(a.year)&&(a.day<=0||a.day>leap_month_day[a.month])) return(false);
    return(true);
}
int main()
{
    date_start.year=2014;date_start.month=9;date_start.day=9;date_start.week=2;
    while(scanf("%d%d%d",&in_month,&in_day,&in_year)!=EOF&&(in_year+in_month+in_day)>0)
    {
        date date_end;date_end.year=in_year;date_end.month=in_month;date_end.day=in_day;
        if(is_valid_date(date_end))
        {
            start_walk_rear(date_start,date_end,comp_date(date_start,date_end));
            printf("%s %d, %d is a %s\n",month_map[date_end.month],date_end.day,date_end.year,week_map[date_end.week]);
        }
        else {printf("%d/%d/%d is an invalid date.\n",date_end.month,date_end.day,date_end.year);}
    }
    return(0);
}

代码2:

#include<stdio.h>
int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//begin with 1
int leap_month_day[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};//begin with 1
typedef struct date
{
    int year;
    int month;
    int day;
    int week;
}date,* date_link;
char week_map[8][10]={"","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};//begin with 1
char month_map[13][10]={"","January","February","March","April","May","June","July","August","September","October","November","December"};//begin with 1
int in_year,in_month,in_day;
date date_start,special_date[5];//begin with 1
bool is_leap(int year)
{
    if(year<=1582) return(year%4==0);
    else return((year%4==0&&year%100!=0)||(year%400==0));
}
int week_mod(int a,int b)
{
    return(a%b?a%b:b);
}
int week_sub(int a,int b)
{
    return(a-b>0?a-b:a-b+7);
}
int week_add(int a, int b)
{
    return(a+b);
}
int comp_date(date a,date b)
{
    int a_num=a.year*10000+a.month*100+a.day;//ip to number to compare ip easily
    int b_num=b.year*10000+b.month*100+b.day;
    return(a_num-b_num);
}
int get_week(date &start,date &rear)
{
    int flag=comp_date(start,rear);
    if(flag>0)
    {
        int total_day=0;
        //start target to start 1/1
        if(start.year>rear.year)
        {
            //complete month
            for(int mi=1;mi<=start.month-1;mi++)
            {
                total_day+=month_day[mi];
                //leap year
                if(mi==2&&is_leap(start.year)) total_day+=1; 
                //special cover the non-existent days
                if(start.year==1582&&mi==10) total_day-=10;
                if(start.year==1752&&mi==9) total_day-=11;
            }
            //complete day
            for(int di=1;di<=start.day-1;di++) total_day++;
            //special cover the non-existent days
            if(start.year==1582&&start.month==10&&start.day>=15&&1<=4) total_day-=10;
            if(start.year==1752&&start.month==9&&start.day>=14&&1<=2) total_day-=11;
        }
        else if(start.year==rear.year)//the dis days is on the same year
        {
            //front
            if(start.month>rear.month)
            {
                for(int di=1;di<=start.day-1;di++) total_day++;
                //special cover the non-existent days
                if(start.year==1582&&start.month==10&&start.day>=15&&1<=4) total_day-=10;
                if(start.year==1752&&start.month==9&&start.day>=14&&1<=2) total_day-=11;
            }
            else if(start.month==rear.month)
            {
                for(int di=start.day-1;di>=rear.day;di--) total_day++;
                //special cover the non-existent days
                if(start.year==1582&&start.month==10&&start.day>=15&&rear.day<=4) total_day-=10;
                if(start.year==1752&&start.month==9&&start.day>=14&&rear.day<=2) total_day-=11;
            }
            //middle
            if(start.month-1>rear.month)
            {
                for(int mi=start.month-1;mi>=rear.month+1;mi--)
                {
                    total_day+=month_day[mi];
                    //leap
                    if(mi==2&&is_leap(start.year)) total_day+=1;
                    //special cover the non-existent days
                    if(start.year==1582&&mi==10) total_day-=10;
                    if(start.year==1752&&mi==9) total_day-=11;
                }
            }
            //rear
            if(start.month>rear.month)
            {
                for(int di=month_day[rear.month];di>=rear.day;di--) total_day++;
                //leap
                if(is_leap(rear.year)&&rear.month==2) total_day+=1;
                //special cover the non-existent days
                if(rear.year==1582&&rear.month==10&&rear.day<=4&&month_day[rear.month]>=15) total_day-=10;
                if(rear.year==1752&&rear.month==9&&rear.day<=2&&month_day[rear.month]>=14) total_day-=11;
            }
        }
        //start to end  complete year
        if(start.year-1>rear.year)
        {
            for(int yi=start.year-1;yi>=rear.year+1;yi--) 
            {
                total_day+=is_leap(yi)? 366 : 365;
                //special cover the non-existent days
                if(yi==1582) total_day-=10;
                if(yi==1752) total_day-=11;
            }
        }
        //end 12/31 to end target
        if(start.year>rear.year)
        {
            for(int mi=12;mi>=rear.month+1;mi--)
            {
                total_day+=month_day[mi];
                //leap
                if(is_leap(rear.year)&&mi==2) total_day+=1;
                //special cover the non-existent days
                if(start.year==1582&&mi==10) total_day-=10;
                if(start.year==1752&&mi==9) total_day-=11;
            }
            for(int di=month_day[rear.month];di>=rear.day;di--) total_day++;
            //leap
            if(is_leap(rear.year)&&rear.month==2) total_day+=1;
            //special cover the non-existent days
            if(rear.year==1582&&rear.month==10&&rear.day<=4&&month_day[rear.month]>=15) total_day-=10;
            if(rear.year==1752&&rear.month==9&&rear.day<=2&&month_day[rear.month]>=14) total_day-=11;
        }
        //calculate the rear's week according to total days
        rear.week=week_mod(week_sub(start.week,week_mod(total_day,7)),7);
    }
    else if(flag<0)
    {
        int total_day=0;
        //start target to end 12/31 the start.day is not in total days not in Dis
        if(start.year<rear.year)
        {
            //complete day
            for(int di=start.day+1;di<=month_day[start.month];di++) total_day++;
            //leap
            if(is_leap(start.year)&&start.month==2) total_day+=1;
            //special cover the non-existent days
            if(start.year==1582&&start.month==10&&month_day[start.month]>=15&&start.day<=4) total_day-=10;
            if(start.year==1752&&start.month==9&&month_day[start.month]>=14&&start.day<=2) total_day-=11;
            //complete month
            for(int mi=start.month+1;mi<=12;mi++)
            {
                total_day+=month_day[mi];
                //leap
                if(is_leap(start.month)&&mi==2) total_day+=1;
                //special cover the non-existent days
                if(start.year==1582&&mi==10) total_day-=10;
                if(start.year==1752&&mi==9) total_day-=11;
            }
        }
        else if(start.year==rear.year)//the dis days is on the same year
        {
            //front
            if(start.month<rear.month)
            {
                for(int di=start.day+1;di<=month_day[start.month];di++) total_day++;
                //leap
                if(is_leap(start.year)&&start.month==2) total_day+=1;
                //special cover the non-existent days
                if(start.year==1582&&start.month==10&&month_day[start.month]>=15&&start.day<=4) total_day-=10;
                if(start.year==1752&&start.month==9&&month_day[start.month]>=14&&start.day<=2) total_day-=11;
            }
            else if(start.month==rear.month)
            {
                for(int di=start.day+1;di<=rear.day;di++) total_day++;
                //special cover the non-existent days
                if(start.year==1582&&start.month==10&&rear.day>=15&&start.day<=4) total_day-=10;
                if(start.year==1752&&start.month==9&&rear.day>=14&&start.day<=2) total_day-=11;
            }
            //middle
            if(start.month+1<rear.month)
            {
                for(int mi=start.month+1;mi<=rear.month-1;mi++)
                {
                    total_day+=month_day[mi];
                    //leap
                    if(is_leap(start.year)&&mi==2) total_day+=1;
                    //special cover the non-existent days
                    if(start.year==1582&&mi==10) total_day-=10;
                    if(start.year==1752&&mi==9) total_day-=11;
                }
            }
            //rear
            if(start.month<rear.month)
            {
                for(int di=1;di<=rear.day;di++) total_day++;
                //special cover the non-existent days
                if(rear.year==1582&&rear.month==10&&1<=4&&rear.day>=15) total_day-=10;
                if(rear.year==1752&&rear.month==9&&1<=2&&rear.day>=14) total_day-=11;
            }
        }
        //start to end  complete year
        if(start.year+1<rear.year)
        {
            for(int yi=start.year+1;yi<=rear.year-1;yi++) 
            {
                total_day+=is_leap(yi)? 366 : 365;
                //special cover the non-existent days
                if(yi==1582) total_day-=10;
                if(yi==1752) total_day-=11;
            }
        }
        //end 12/31 to end target
        if(start.year<rear.year)
        {
            for(int mi=1;mi<=rear.month-1;mi++)
            {
                total_day+=month_day[mi];
                //leap
                if(is_leap(rear.year)&&mi==2) total_day+=1;
                //special cover the non-existent days
                if(start.year==1582&&mi==10) total_day-=10;
                if(start.year==1752&&mi==9) total_day-=11;
            }
            for(int di=1;di<=rear.day;di++) total_day++;
            //special cover the non-existent days
            if(rear.year==1582&&rear.month==10&&1<=4&&rear.day>=15) total_day-=10;
            if(rear.year==1752&&rear.month==9&&1<=2&&rear.day>=14) total_day-=11;
        }
        //calculate the rear's week according to total days
        rear.week=week_mod(week_add(start.week,week_mod(total_day,7)),7);
    }
    else//start == rear
    {
        rear.week=start.week;
    }
    return(0);
}
bool is_valid_date(date a)
{
    if(a.year<=0) return(false);
    if(a.month<=0||a.month>=13) return(false);
    if(!is_leap(a.year)&&(a.day<=0||a.day>month_day[a.month])) return(false);
    if(is_leap(a.year)&&(a.day<=0||a.day>leap_month_day[a.month])) return(false);
    return(true);
}
int main()
{
    special_date[1].year=1582;special_date[1].month=10;special_date[1].day=4;special_date[1].week=4;
    special_date[2].year=1582;special_date[2].month=10;special_date[2].day=15;special_date[2].week=5;
    special_date[3].year=1752;special_date[3].month=9;special_date[3].day=2;special_date[3].week=3;
    special_date[4].year=1752;special_date[4].month=9;special_date[4].day=14;special_date[4].week=4;
    date_start.year=2014;date_start.month=9;date_start.day=9;date_start.week=2;
    while(scanf("%d%d%d",&in_month,&in_day,&in_year)!=EOF&&(in_year+in_month+in_day)>0)
    {
        date date_end;
        date_end.year=in_year;
        date_end.month=in_month;
        date_end.day=in_day;
        if(is_valid_date(date_end))
        {
            get_week(date_start,date_end);
            printf("%s %d, %d is a %s\n",month_map[date_end.month],date_end.day,date_end.year,week_map[date_end.week]);
        }
        else
        {
            printf("%d/%d/%d is an invalid date.\n",date_end.month,date_end.day,date_end.year);
        }
    }
    return(0);
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值