题解 | #今年的第几天?#

今年的第几天?

https://www.nowcoder.com/practice/ae7e58fe24b14d1386e13e7d70eaf04d

#include <iostream>
using namespace std;
int judge_year(int year){
    int count = 0;
    if(year%4==0&&year%100!=0||year%400==0){
        return 1;
    }
    else return 0;
}
int main() {
    int year,mouth,day;
    int num1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int num2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    while (cin >> year >> mouth>>day) { // 注意 while 处理多个 case
        int count = 0;
        int flag = judge_year(year);
        if(flag){
            for(int i = 0;i<mouth-1;i++){
                count+=num2[i];
            }
        }
        else{
            for(int i = 0;i<mouth-1;i++){
                count+=num1[i];
            }
        }
        count+=day;
        cout<<count<<endl;
    }
}

// 64 位输出请用 printf("%lld")

设置两个数组,分别存储每个月的天数,但是要考虑是否为闰年。所以先判断,之后再按月份加到之前一个月,然后再加上当月天数。

全部评论

相关推荐

ResourceUt...:楼主有自己的垃圾箱,公司也有自己的人才库
点赞 评论 收藏
分享
牛客83265014...:完了,连现在都没开始面,13号投的是不是晚了
秋招的第一个offer,...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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