codeup100000578B-DayofWeek

来源:互联网 发布:阿里办公软件 编辑:程序博客网 时间:2024/06/06 02:28

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.

输入样例:

21 December 2012
5 January 2013

输出样例:

Friday
Saturday

题目链接


思路:

题意:就是判断输入日期是星期几

1.用二维数组来存放月份天数,月份名称和星期名称:

//注意要把month[0][2]空出来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 s_month[13][15] = { " ","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };//二维数组存储星期名称,星期数组不用留空char week[7][15] = { "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday" };

2.取一个标杆时间(如2016-8-15 星期一)与前面的日期差值题目类似,time1不断累加,直到与time2相同为止。获得中间的天数差值cnt
3.用cnt%7获得与星期一的天数差(6以内),之后再修正

        if (!flag)  //输入日期在2016-8-15之后            printf("%s\n", week[cnt % 7]);        else    //输入日期在2016-8-15之前            printf("%s\n", week[6 - (cnt-1) % 7]);

代码

/*** @tag     null* @authors R11happy (xushuai100@126.com)* @date* @version 1.0* @Language C++* @Ranking  null* @function 判断某天是星期几*/#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>using namespace std;//注意要把month[0][2]空出来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 s_month[13][15] = { " ","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };//二维数组存储星期名称,星期数组不用留空char week[7][15] = { "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday" };//判断是否为闰年bool  isLeap(int year){    return  ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));}//通过月份名称获取月份下标int getMonth(char* str){    int i = 0;    for (i = 1; i<13; i++)    {        if (strcmp(str, s_month[i]) == 0)        {            return i;        }    }    return -1;}int main(int argc, char const *argv[]){    int time1, y1, m1, d1;    int time2, y2, m2, d2;    char str[15];    while (scanf("%d %s %d", &d2, &str, &y2) != EOF)    {        int flag = 0;        int cnt = 0;        m2 = getMonth(str);        time1 = 20160815;   //存储当前时间(星期一)作为标杆        time2 = y2 * 10000 + m2 * 100 + d2;        //保证time1时间比较早        if (time1 > time2)        {            flag = 1;            swap(time1, time2);        }        //更新交换后的时间        y1 = time1 / 10000;        m1 = time1 % 10000 / 100;        d1 = time1 % 100;        y2 = time2 / 10000;        m2 = time2 % 10000 / 100;        d2 = time2 % 100;        //time1不断加1,直到与time2时间相同为止        while (y1 != y2 || m1 != m2 || d1 != d2)        {            d1++;            //更新下个月时间            if (d1 == month[m1][isLeap(y1)] + 1)            {                m1++;                d1 = 1;            }            //更新下一年时间            if (m1 == 13)            {                y1++;                m1 = 1;            }            cnt++;        }        if (!flag)  //输入日期在2016-8-15之后            printf("%s\n", week[cnt % 7]);        else    //输入日期在2016-8-15之前            printf("%s\n", week[6 - (cnt-1) % 7]);    }    return 0;}

收获

1.名称最好用char的二维数组来存,方便处理
2.由于代码是取星期一作为标杆,在修正时候2016-8-15之后的日期比较好处理,之前的日期要想将cnt-1,再取余处理

        else    //输入日期在2016-8-15之前            printf("%s\n", week[6 - (cnt-1) % 7]);
0 0
原创粉丝点击