欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-26 17:56:12
//#include "stdafx.h" #include using namespace std; bool isleapyear(unsigned int...
//#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;
}