Checking the Calendar
You are given names of two days of the week.
Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong to one year.
In this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.
Names of the days of the week are given with lowercase English letters: "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".
Input
The input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".
Output
Print "YES" (without quotes) if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes).
Examples
Input
monday tuesday
Output
NO
Input
sunday sunday
Output
YES
Input
saturday tuesday
Output
YES
Note
In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays.
In the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int f(char *);
int main()
{
int b[2], sum;
sum = -1;
char a[2][20];
scanf("%s", a[0]);
scanf("%s", a[1]);
b[0] = f(a[0]);
b[1] = f(a[1]);
if(b[0] >= b[1]) sum = b[0] - b[1];
else sum = b[1] - b[0];
if(sum == 0 || sum == 2 || sum == 3) printf("YES\n");
else printf("NO\n");
return 0;
}
int f(char *a)
{
int b;
if (strcmp(a, "monday")) b = 1;
else if(strcmp(a, "tuesday")) b = 2;
else if(strcmp(a, "wednesday")) b = 3;
else if(strcmp(a, "thursday")) b = 4;
else if(strcmp(a, "friday")) b = 5;
else if(strcmp(a, "saturday")) b = 6;
else if(strcmp(a, "sunday")) b = 7;
return b;
}
代码提交是Wrong answer ,原因:
1. 下方函数的strcmp使用错误,当所比较的字符串相等时,其返回值为0,我误以为和if函数一样,相等时为1
2. sum的计算不能够只计算时间间隔,一个星期的两天可以有:从前面向后数和从后面向前面数两种方式,所以两天的间隔可能有两个结果,此处只能用后一天减前一天
改正代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int f(char *);
int main()
{
int b[2], sum;
sum = -1;
char a[2][20];
scanf("%s", a[0]);
scanf("%s", a[1]);
b[0] = f(a[0]);
b[1] = f(a[1]);
sum = b[1] - b[0];
//计算后一天和前一天差几天,一定要用后一天减前一天!不要减反!!!
if(sum < 0) sum += 7;//如果sum<0则进入下一个星期
if(sum == 0 || sum == 2 || sum == 3) printf("YES\n");
//每个月有28或30或31天三种可能,相当于间隔0或2或3天,所以sum的三种结果符合要求
else printf("NO\n");
return 0;
}
int f(char *a)
{
int b;
if (strcmp(a, "monday") == 0) b = 1;
else if(strcmp(a, "tuesday") == 0) b = 2;
else if(strcmp(a, "wednesday") == 0) b = 3;
else if(strcmp(a, "thursday") == 0) b = 4;
else if(strcmp(a, "friday") == 0) b = 5;
else if(strcmp(a, "saturday") == 0) b = 6;
else if(strcmp(a, "sunday") == 0) b = 7;
return b;
}