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];
    }
};
相关推荐
转调几秒前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
不穿格子衬衫29 分钟前
常用排序算法(下)
c语言·开发语言·数据结构·算法·排序算法·八大排序
wdxylb36 分钟前
使用C++的OpenSSL 库实现 AES 加密和解密文件
开发语言·c++·算法
aqua353574235838 分钟前
蓝桥杯-财务管理
java·c语言·数据结构·算法
CV金科38 分钟前
蓝桥杯—STM32G431RBT6(IIC通信--EEPROM(AT24C02)存储器进行通信)
stm32·单片机·嵌入式硬件·算法·蓝桥杯
sewinger1 小时前
区间合并算法详解
算法
CSP126361 小时前
特别节目————集训总结
c++
XY.散人1 小时前
初识算法 · 滑动窗口(1)
算法
huanxiangcoco1 小时前
152. 乘积最大子数组
python·leetcode
程序猿阿伟1 小时前
《C++游戏人工智能开发:开启智能游戏新纪元》
c++·人工智能·游戏