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

计算日期到天数转换

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

判断闰年 https://www.zhihu.com/question/25388501

#include<bits/stdc++.h>
using namespace std;

// 判断是否是闰年,闰年2月29天,平年2月28天
int is_leap_year(int year){
    if((year%4==0 && year%100!=0) || (year%400==0)){
        return 29;
    }else{
        return 28;
    }
}

// 定义月份
int days[] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main(){
    string str;
    int year, month, day,count;
    while(getline(cin,str)){
        count = 0;
        int pos = str.find(' ', 0);
        year = stoi(str.substr(0, pos));

        int flag = pos + 1;
        pos = str.find(' ', flag);
        month = stoi(str.substr(flag, pos - flag));

        day = stoi(str.substr(pos+1));
        days[1] = is_leap_year(year);
        for (int i = 0; i < month-1;i++){
            count += days[i];
        }
        count += day;
        cout << count << endl;
    }

    return 0;
}
全部评论

相关推荐

LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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