LeetCode每日一题——1185. Day of the Week

文章目录

一、题目

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Example 1:

Input: day = 31, month = 8, year = 2019

Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999

Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993

Output: "Sunday"

Constraints:

The given dates are valid dates between the years 1971 and 2100.

二、题解

使用基姆拉尔森公式计算

cpp 复制代码
class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        vector<string> res = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        if(month == 1 || month == 2) month += 12,year--;
        int week = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
        return res[week];
    }
};
相关推荐
泽020212 分钟前
C++入门(缺省参数/函数/引用)
开发语言·c++
m0_5041353012 分钟前
代码随想录算法训练营第六十一天 | floyd算法
算法
xin007hoyo4 小时前
算法笔记.染色法判断二分图
数据结构·笔记·算法
mozun20204 小时前
VS BUG(6) LINK : fatal error LNK1158: 无法运行“rc.exe”
c++·bug·vs·链接器·资源文件
whoarethenext5 小时前
初始https附带c/c++源码使用curl库调用
服务器·c++·qt·https·curl
cloues break.6 小时前
C++进阶----多态
开发语言·c++
এ᭄画画的北北6 小时前
力扣-234.回文链表
算法·leetcode·链表
Despacito0o7 小时前
C++核心编程:类与对象全面解析
开发语言·c++
八股文领域大手子7 小时前
深入理解缓存淘汰策略:LRU 与 LFU 算法详解及 Java 实现
java·数据库·算法·缓存·mybatis·哈希算法
__lost8 小时前
C++ 解决一个简单的图论问题 —— 最小生成树(以 Prim 算法为例)
算法·图论·最小生成树·prim算法