题目
给出一个日期,输出对应是星期几,思路:计算距离今天是几天,然后一推导,完事儿,这里的“今天”是2022年1月18日 周二
牛客网题目链接:题目
KY108 Day of Week
中等
通过率:27.62% 时间限制:1秒
空间限制:64M
知识点
模拟
基础数学
思维
warning 校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
描述
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 2005, 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. 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
示例1
输入:
9 October 2001
14 October 2001
输出:
Tuesday
Sunday
前言
我的代码有点过于繁琐,但是还是挺好理解的!
写了差不多两小时才AC,WA了一次,调了三个bug才过,
bug1:第15行,判断闰年函数写错一行,成return false
bug2:第59行,11月的英文写错。。。(题目都给了所有单词了阿喂,但是我没看到,以为要默写呢,这考小学英语,然后一个字“寄”…一开始我月份和星期拼不熟练百度抄的,哈哈哈哈抄还抄错)
bug3:第113行,往后的日期,偏移量是减去7才对。一开始减6,又由于就影响了周一,故WA通过了17/20组数据哈哈哈
代码
#include <iostream>
#include <cstdio>
using namespace std;
int MONTH[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 1 2 3 4 5 6 7 8 9 10 11 12
bool is_leap(int year) {
if (year % 100 == 0) {
if (year % 400 == 0)
return true;
} else {
if (year % 4 == 0)
return true;
}
return false;
}
//日期差值, 大的日期-小的日期
int sub(int big_year, int big_mon, int big_day, int small_year, int small_mon, int small_day) {
int ans = 0;
//计算年份的 日期差值
for (int i = small_year; i <= big_year - 1; ++i) {
if (is_leap(i))
ans += 366;
else
ans += 365;
}
//计算天数的 日期差值
//先计算大的日期是第几天
int num_big = big_day;
if (is_leap(big_year))
MONTH[2] = 29;
else
MONTH[2] = 28;
for (int i = 1; i < big_mon; ++i) {
num_big += MONTH[i];
}
//再计算小的日期是第几天
int num_small = small_day;
if (is_leap(small_year))
MONTH[2] = 29;
else
MONTH[2] = 28;
for (int i = 1; i < small_mon; ++i) {
num_small += MONTH[i];
}
//ans加上(大-小)即可
ans = ans + (num_big - num_small);
return ans;
}
int main() {
//距离今天多少天,然后推星期几,完事
string mon[13] = {"None", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
string week[8] = {"None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//今天 2022年1月18日20:12:27 2022年1月18日 星期二
int day, year, month = 0;
string month_str;
while (cin >> day >> month_str >> year) {
//获取月份
for (int i = 1; i <= 12; ++i) {
if (month_str == mon[i]) {
month = i;
break;
}
}
//输入的日期与当前 2022-1-18比较
if (20220118 >= year * 10000 + month * 100 + day) {
int diff = sub(2022, 1, 18, year, month, day);
//往前推星期几咯
diff %= 7;
/*diff =
0-周2
1-周1
2 周7
3 周6
4-周5
5-周4
6-周3
总结规律:周(2-diff),if(2-diff<=0) 加上7
*/
// printf("diff=%d\n", diff);
int ans_weekday = 2 - diff;
if (ans_weekday <= 0)
ans_weekday += 7;
cout << week[ans_weekday] << endl;
// cout << "星期;" << ans_weekday << endl;
} else {
int diff = sub(year, month, day, 2022, 1, 18);
//往后推星期几咯
diff %= 7;
/*diff =
0-周2
1-周3
2 周4
3 周5
4-周6
5-周7
6-周1
总结规律:周(2+diff),if(2+diff>=8) 减7
*/
// printf("diff=%d\n", diff);
int ans_weekday = 2 + diff;
if (ans_weekday >= 8)
ans_weekday -= 7;
cout << week[ans_weekday] << endl;
// cout << "星期;" << ans_weekday << endl;
}
}
// int a, b, c, d, e, f;
// while (cin >> a >> b >> c >> d >> e >> f) {
// cout << sub(a, b, c, d, e, f) << endl;
// }
return 0;
}
567

被折叠的 条评论
为什么被折叠?



