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];
    }
};
相关推荐
人类发明了工具2 分钟前
【机器人-激光雷达】点云时间运动补偿
算法·机器人
小杰帅气5 分钟前
红黑树实现
数据结构
f***019324 分钟前
CC++链接数据库(MySQL)超级详细指南
c语言·数据库·c++
合方圆~小文33 分钟前
球型摄像机作为现代监控系统的核心设备
java·数据库·c++·人工智能
north_eagle1 小时前
向量搜索技术深度研究报告:架构原理、核心算法与企业级应用范式
算法·架构
椰萝Yerosius2 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
小曹要微笑2 小时前
STM32F7 时钟树简讲(快速入门)
c语言·stm32·单片机·嵌入式硬件·算法
南山安2 小时前
栈(Stack):从“弹夹”到算法面试题的进阶之路
javascript·算法·面试
2301_764441333 小时前
Python构建输入法应用
开发语言·python·算法
AI科技星3 小时前
为什么变化的电磁场才产生引力场?—— 统一场论揭示的时空动力学本质
数据结构·人工智能·经验分享·算法·计算机视觉