当前位置: 移动技术网 > IT编程>开发语言>C/C++ > ACM日期计算(C++实现):输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天

ACM日期计算(C++实现):输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天

2018年10月08日  | 移动技术网IT编程  | 我要评论

泥鳅的药用价值,儿歌视频串烧,疯狂宠物2

//#include "stdafx.h"
#include 
using namespace std;

bool isleapyear(unsigned int year) {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        return true;
    }
    else {
        return false;
    }
}

int getdays(unsigned int year, unsigned int month, unsigned int day) {
    if (month == 0 || month > 12 || day == 0 || day > 31) {
        return -1;
    }
    int days = 0;//天数
    switch (month - 1) {//累加本月之前的天数
    case 11:
        days += 30;
    case 10:
        days += 31;
    case 9:
        days += 30;
    case 8:
        days += 31;
    case 7:
        days += 31;
    case 6:
        days += 30;
    case 5:
        days += 31;
    case 4:
        days += 30;
    case 3:
        days += 31;
    case 2:
        days += isleapyear(year) == true ? 29 : 28;//若是闰年则+ 29,否则+ 28
    case 1:
        days += 31;
    }
    days += day;//加上本月天数
    return days;
}

int main() {
    int n = 0;
    cin >> n;
    while (n--) {
        unsigned int year = 0;
        unsigned int month = 0;
        unsigned int day = 0;
        cin >> year >> month >> day;
        cout << getdays(year, month, day) << endl;
    }

    //system("pause");
    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网