华为oj中级 计算日期到天数转换

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_map>
#include<fstream>
#include<sstream>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<utility>
#include<iomanip>
using namespace std;
unsigned int num_day;
int a[11] = { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
int b[11] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
bool is_leap(unsigned int Y){
    if ((Y % 4 == 0) || ((Y % 100 == 0 )&& (Y % 400 == 0))) return true;
    return false;
}
bool time_to_day(unsigned int y, unsigned int m, unsigned int d){
    if (m > 12 || d > 31 ) return false;
    //判断平年和闰年
    if (is_leap(y)){

        if (m == 1){
            num_day = d;
            return true;
        }
        else{
            num_day = a[m - 2] + d;
            return true;
        }
    }
    else{
        if (m == 1){
            num_day = d;
            return true;
        }
        else{
            num_day = b[m - 2] + d;
            return true;
        }
    }

}
int main() {
    unsigned int year, month, day;
    while (cin >> year >> month >> day){
        bool flag = time_to_day(year, month, day);
        if (flag){
            cout << num_day << endl;
        }
        else{
            cout << -1 << endl;
        }

    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值