您的位置:首页 > 其它

日期2

2016-01-30 17:52 148 查看
/*

题目 1043 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.

样例输入:

9 October 2001

14 October 2001

样例输出:

Tuesday

Sunday

提示:

Month and Week name in Input/Output:

January, February, March, April, May, June, July, August, September, October, November, December

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

*/

#include <stdio.h>

#include <string.h>

#define isyear(x) x%100 != 0 && x % 4 == 0 || x % 400 == 0 ? 1:0

//定义宏判断是否为闰年,方便计算每月天数

int dayofmonth[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

};//预存每月的天数,注意二月配合宏定义作特殊处理

struct date{//日期类,方便日期的推移

    int day;

    int month;

    int year;

    void nextday(){//计算下一天的日期

        day++;

        if(day > dayofmonth[month][isyear(year)]){

        //若日数超过了当月最大日数

            day = 1;

            month++;//进入下一月

            if(month>12){//月数超过12

                month = 1;

                year ++;//进入下一年

             

            }

        }

    }

 

};

int buf[5001][13][32];//保存预处理的天数,因为题目要求,可以只初始化到3001(1000<=y<=3000)

char monthname[13][20]={

"",

"January",

"February",

"March",

"April",

"May",

"June",

"July",

"August",

"September",

"October",

"November",

"December"

};//月名,每个月名对应下标1到12

char weekname[7][20] ={

"Sunday",

"Monday",

"Tuesday",

"Wednesday",

"Thursday",

"Friday",

"Saturday"

};//周名,每个周名对应下标0到6

 

int main(){

    date tmp;

    int cnt = 0;//天数计数

    tmp.day = 1;

    tmp.month=1;

    tmp.year=0;//初始化日期类对象为0年1月1日

    while(tmp.year != 5001){//日期不超过5000年

        buf[tmp.year][tmp.month][tmp.day] = cnt;

        //将该日与0年1月1日的天数差保存起来

        tmp.nextday();//计算下一天日期

        cnt++;//计数增加,每经过一天计数器即+1,代表与原点日期的间隔又增加了一天

    }

    int d,m,y;

    char s[20];

    while(scanf("%d%s%d",&d,&s,&y) != EOF){

        for(m=1;m<=12;m++){

            if(strcmp(s,monthname[m])==0){

              break;//将输入字符串与月名比较得出月数

            }

        }

        int days = buf[y][m][d] - buf[2012][7][16];

        //计算给定日期与今日日期的天数间隔(注意可能为负)

        days += 1;//今天2012.7.16为星期一,对应数组下标为1,则计算1经过days天后的下标

        puts(weekname[(days % 7 + 7) % 7]);//将计算后得出的下标用7对其取模,并且保证其为非负数,则该下标即为答案所对应的下标,输出即可

    }

    return 0;

}

#include <stdio.h>

#include <string.h>

 

int dayOfMonth[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 monthName[13][20] = {

    " ",

    "January",

    "February",

    "March",

    "April",

    "May",

    "June",

    "July",

    "August",

    "September",

    "October",

    "November",

    "December"

};

char weekName[8][20] = {

    " ",

    "Sunday",

    "Monday",

    "Tuesday",

    "Wednesday",

    "Thursday",

    "Friday",

    "Saturday"

};

//判断闰年

int leap_year(int year){

    if((year%100 != 0 && year%4 == 0) || (year % 400 == 0)){

        return 1;

    }

    else{

        return 0;

    }

}

//计算天数

int TDay(int day,char month[],int year){

 

    int day1 = 0,i,j;

    int isleap_year = leap_year(year);

 

    for(i = 1;i< year;i++){  

        if(leap_year(i)){  

            day1 += 366;  

        }  

        else{  

            day1 += 365;  

        }  

    }  

 

    for(i = 1;i < 13;i++){

        if(strcmp(month,monthName[i]) == 0){

            break;

        }

    }

    for(j = 1;j < i;j++){

        day1 +=  dayOfMonth[j][isleap_year];

    }

     

    day1 += day;

 

    return day1%7+1;

}

 

int main()

{

    int day,year;

    char month[20];

    //freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin); 

    while(scanf("%d%s%d",&day,month,&year) != EOF)

    {

        printf("%s\n",weekName[TDay(day,month,year)]);

    }

    return 0;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: