赞
踩
今天的每日一题:2022. 将一维数组转变成二维数组 - 力扣(LeetCode) (leetcode-cn.com)
class Solution { public: vector<vector<int>> construct2DArray(vector<int> &original, int m, int n) { vector<vector<int>> ans(m,vector<int>(n)); if (original.size() != m * n) { return {}; } int index =0; for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { ans[i][j]=original[index++]; } } return ans; } };
今天的每日一题是:390. 消除游戏 - 力扣(LeetCode) (leetcode-cn.com)
class Solution { public: int lastRemaining(int n) { int a1 = 1; int k = 0, cnt = n, step = 1; while (cnt > 1) { if (k % 2 == 0) { // 正向 a1 = a1 + step; } else { // 反向 a1 = (cnt % 2 == 0) ? a1 : a1 + step; } k++; cnt = cnt >> 1; step = step << 1; } return a1; } };
今天的每日一题是:一周中的第几天
给定day, month, year来输出week
**方法一:**使用模拟
即通过规律来推导出当天距1970年一共有多少天,然后+3(1970年最后一天是周四,数组下标为3)%7(weekday)得出是一周的第几天。
然而实际编写代码的过程中出现了问题,实际得出天数差值少了2;
**原因是:**当最后一年为世纪闰年时(如2000),最后一个月之前的所有月份都被当成了29天来算。
f(m==2&&(year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
可以看到 || year % 4000 == 0;导致当最后一年为世纪闰年时前面的m==2不起作用。
#include<iostream> #include<string.h> #include<vector> using namespace std; int dayOfTheWeek(int day, int month, int year) { int dif = 0; //1970年最后一天是周四,下标为3 vector<string> week={ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"}; vector<int> monthDay={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //初始化对应数组 //判断闰年闰年有366天,平年有365天 for(int y=1971;y<year;++y) { bool isleap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; dif += isleap?366:365; } //cout<<dif<<endl;//10592 for(int m=1;m<month;++m)//这里少了两天【BUG】 { if(m==2&&((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { dif += 29; } else { dif += monthDay[m-1]; } if(m==1) cout<<dif<<endl; } cout<<dif<<endl; dif=dif+day+3; int w=dif % 7; return dif; } int main() { cout<<dayOfTheWeek(29,2,2000)<<endl;//10653 X 10655(29有7是闰) 最后是10652+3 system("pause"); }
**方法二:**根据菜勒公式来推出时间:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-70O6XoWl-1649921051332)(https://bkimg.cdn.bcebos.com/formula/65717a6a02c703f5ce22f59d077da3ff.svg)]
今天的每日一题是:913. 猫和老鼠
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
class Solution {
public:
int n;
int helper(VVI& graph, int t, int x, int y, VVVI& dp){
if (t == 2 * n)
return 0;
if (x == y)
return dp[t][x][y] = 2;
if (x == 0)
return dp[t][x][y] = 1;
if (dp[t][x][y] != -1)
return dp[t][x][y]; // “不要搜了,爷声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/615510
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。