Codeforces

电玩女神 2022-07-16 06:16 320阅读 0赞

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)

1.以上为题目原题,由于我英文水平太差,读题就耗费了我一些时间!终于大概知道了题意,题意为:

给你一周中的两个时间,如周一,周二,让你写个程序判断一下存不存在让周一为某一平年的某一个月的第一天,而周二恰好为下个月的第一天。并且是在同一年内。如果存在则输出YES,否则输出NO。

2。解题思路:我们可以从第一个月开始判断,逐一递推到十一月。

3.参考代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. char s1[15],s2[15];
  6. char s[7][15]={"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
  7. int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
  8. int i,k,m;
  9. cin>>s1>>s2;
  10. for(i=0;i<7;i++)
  11. {
  12. if(strcmp(s[i],s1)==0)
  13. {
  14. k=i+1;
  15. }
  16. if(strcmp(s[i],s2)==0)
  17. {
  18. m=i+1;
  19. }
  20. }
  21. int flag=0,temp,p;
  22. for(i=0;i<11;i++)
  23. {
  24. temp=a[i]%7;
  25. if(k+temp==7)
  26. {
  27. p=k+temp;
  28. }
  29. else
  30. {
  31. p=(k+temp)%7;
  32. }
  33. if(p==m)
  34. { cout<<"YES"<<endl;
  35. flag=1;
  36. break;}
  37. }
  38. if(flag==0)
  39. cout<<"NO"<<endl;
  40. return 0;
  41. }

4.本题应注意的问题:

(1)字符串是不能直接比较的,进行字符串比较应调用相应的函数strcmp()。

(2)(k+temp)%7应注意当(k+temp)等于7时则不应该进行求余运算,否则将导致错误的结果。

发表评论

表情:
评论列表 (有 0 条评论,320人围观)

还没有评论,来说两句吧...

相关阅读