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

计算一年中的第几天

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

#include <bits/stdc++.h>
using namespace std;
//判断闰年平年, 每月天数,4 6 9 11,四个月为小月, 7个月为大月1 3 5 7 8 10 12, 一个特殊月,闰年29天,平年28天
int moth[13]={0,31, 28,31, 30, 31,30,31,31,30,31,30,31};
int main() {
    int Y, M ,D;
    while(cin >>Y >> M >> D){
        //判断,能被400整除 或者能被4整除,但不能被100整除的叫2月28天
        //初始化
        int  days = 0;
        moth[2] = 28;
        if(Y%400 == 0 || (Y%4==0 && Y%100 != 0)){//闰年判断条件
            moth[2] = 29;
        }
        //计算天数
        for(int i =1; i<M; i++){
            days += moth[i];
        }
        days += D;
        cout << days << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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