日期差值题目(也可能是最容易看懂的了)
题目
思路 :
我们是了许多的方法比如去相减,然后考虑到了借位的问题,还是比较的麻烦,所以我们直接去计算到公园1年的日期,月就是绝对值的日期,最后我们再去相减
答案:
我用的比较简单的方法,其是confess可以不用写,而是直接去写从1到没个月的天数
#include <iostream>
using namespace std;
int confess(int month,int * ripple)
{
int ret = 0;
for (int i = 0; i < month; i++)
{
ret += ripple[i];
}
return ret;
}
int mystic(int a1, int b1, int c1,int ret)
{
int mangy = a1 * 365 + a1 / 400 + a1 / 4 - a1 / 100;
if (b1 > 2 && (a1 % 400 == 0) || b1 > 2 && (a1 % 4 == 0 && a1 % 100 != 0))
{
mangy += 1;
}
mangy += ret;
mangy += c1;
return mangy;
}
int main() {
int a, b;
int ripple[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
while (cin >> a>>b )
{
int a1, a2, b1, b2, c1, c2;
a1 = a2 = b1 = b2 = c1 = c2 = 0;
c1 = a % 100;
a /= 100;
b1 = a % 100;
a /= 100;
a1 = a;
c2 = b % 100;
b /= 100;
b2 = b % 100;
b /= 100;
a2 = b;
int ret1 = confess(b1, ripple);
int ret2 = confess(b2, ripple);
int pause1 = mystic(a1,b1,c1,ret1);
int pause2 = mystic(a2,b2,c2,ret2);
cout << abs(pause1 - pause2) + 1 << endl;//这个+1还挺重要
//cout << a1 << " " << b1 << " " << c1 << " " << endl << a2 << " " << b2 << " " << c2;
//cout << ret1<<ret2;
}
}