题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

#include <iostream>
using namespace std;

int main() {
    int a, b;
    int day;
    int arr1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int arr2[12]={31,29,31,30,31,30,31,31,30,31,30,31};

    while (cin >> a >> b) { // 注意 while 处理多个 case
        cin>>day;
        int sum=0;
        if((a%4==0&&a%100!=0)||(a%400==0))
        {
            for(int i=0;i<b-1;i++)//闰年
            {
                sum+=arr2[i];
            }
            sum+=day;
        }
        else
        {
            for(int i=0;i<b-1;i++)//非闰年
            {
                sum+=arr1[i];
            }
            sum+=day;
        }
        cout << sum << endl;
        
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务