题解 | 计算一年中的第几天

计算一年中的第几天

https://www.nowcoder.com/practice/178aa3dafb144bb8b0445edb5e9b812a

Modern Cpp

#include <iostream>
#include <array>
#include <numeric>

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

  auto isLeap = [](int year){
    if(year % 100 && year % 4 == 0){
      return true;
    }
    if(year % 400 == 0){
      return true;
    }
    return false;
  };

  constexpr std::array days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  int y, m, d;
  while(std::cin >> y >> m >> d){
    int ans = std::accumulate(days.begin(), days.begin() + m, d);
    if(m > 2 && isLeap(y)){
      ans++;
    }
    std::cout << ans << "\n";
  }

  return 0;
}

全部评论

相关推荐

xtu大迫杰:偶遇校友,祝校友offer打牌
点赞 评论 收藏
分享
04-03 22:41
兰州大学 C++
老六f:有时候是HR发错了,我之前投的百度的后端开发,他给我发的算法工程师,但是确实面的就是百度开发
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务