设为首页 加入收藏

TOP

ACM日期计算(C++实现):输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天
2018-05-08 06:06:49 】 浏览:200
Tags:ACM 日期 计算 实现 输入 一个日期 格式 2010 判断 一年中
//#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;
}
  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++算法之冒泡排序、选择排序算法.. 下一篇C++相关数据结构成员方法的调用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目