问题 A: 日期差值(求两日期间相差天数)
程序员文章站
2022-03-22 10:14:21
题目链接#include #include #include using namespace std;int mouth[13][2] ={ {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {3...
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int mouth[13][2] =
{
{0, 0}, {31, 31}, {28, 29}, {31, 31},
{30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31},
{30, 30}, {31, 31}, {30, 30}, {31, 31}
}; // 区分平年和闰年每个月的天数
bool isLeap(int year) // 判断平闰年
{
return (year%400 == 0 || (year % 4 == 0 && year % 100 != 0));
}
int main()
{
int time1, y1, m1, d1;
int time2, y2, m2, d2;
while (~scanf("%d%d", &time1, &time2))
{
if (time1 > time2) // 找到较小的日期
{
int tmp = time1;
time1 = time2;
time2 = tmp;
}
y1 = time1 / 10000, m1 = time1%10000/100, d1 = time1%100;
y2 = time2 / 10000, m2 = time2%10000/100, d2 = time2%100;
int ans = 1;
while (y1 < y2 || m1 < m2 || d1 < d2)
{
d1++;
if (d1 == mouth[m1][isLeap(y1)]+1)
{
m1++;
d1 = 1;
}
if (m1 == 13)
{
y1++;
m1 = 1;
}
ans++;
}
printf("%d\n", ans);
}
return 0;
}
本文地址:https://blog.csdn.net/qq_43545471/article/details/109250763