问题:
一年中任意两天差值,输入N组数据进行测试时,输出这两天之间的间隔天数,第二个日期总是在比第一个日期要大。
例如,输入:
2
2019 1 1 2019 1 2
2016 1 1 2016 3 1
输出:
2
61
分析:
同一年进的两个日期之间计算时间,则分别计算出每个日期是这一年的第几天,直接相减加1即可。比如第一个测试用例,19年的第一天和第二天时间间隔为多少,相减加1。为2 - 1 + 1 = 2天。
计算某一日期是一年中的第几天,直接采用暴力循环计算。
代码:
/*
计算同一年两个日期的时间间隔
*/
#include<iostream>
using namespace std;
int total = 0;
int computing(int year, int month, int day)
{
int total = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
if (month == 1)
{
total += day;
}
else if (month == 2)
total = 31 + day;
else if (month == 3)
total = 31 + 29 + day;
else if (month == 4)
total = 31 + 29 + 3