欢迎来到 A_Dull_Rabbit 的博客

数据结构实训(二)--- (清华大学机考)今年的第几天?

【问题描述】输入年、月、日,计算该天是本年的第几天。
【输入形式】包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。
【输出形式】输入可能有多组测试数据,对于每一组测试数据,输出一个整数,代表Input中的年、月、日对应本年的第几天。
【样例输入】1990 9 20
【样例输出】263
【样例说明】

#include<iostream>
using namespace std;


// 判断闰年 如果是闰年
bool Judge_year(int i)
{
    return (i%100!=0&&i%4==0) || (i%400==0);
}

int main()
{
    int year, month, day;   // 年月日
    int dayInFeb;           // 二月的天数

    cin >> year >> month >> day;

    if(Judge_year(year))    // 是闰年
    {
       dayInFeb = 29;
    }
    else                    // 不是闰年
    {
       dayInFeb = 28;
    }

    // 每月天数
    int dayMonth[12]={31, dayInFeb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // 统计天数
    int sum = 0;

    for(int i=0; i<month-1; i++){
        sum += dayMonth[i];
    }

    sum += day;

    cout << sum;

}

 

posted on 2020-03-24 14:09  A_Dull_Rabbit  阅读(205)  评论(0编辑  收藏  举报

导航