poj2210

题意:把时间转换为公制,给普通的出年月日时分秒,然后从2000/1/1开始是公制的时间起始点,每天有10小时,每小时有100分,每分有100秒计算,问给出的普通的时间点,在公制中对应的时间点的公制表示是多少。

分析:难点在于给出年月日,统计过了多少天。

本题以2000年为起点恰为闰年。我们可以先将2000年刨除,从2001年开始,2001年是一个崭新的400年的开头,所以就恰好符合除法规律,例如2008年是2001年开始的第8年,8/4=2,2001~2008出现过2个闰年,2009年,9/4=2,出现过两个闰年。所以从2001年起(包括2001年)到n年(包括n年),所包含的闰年数可以直接通过除法求得,设x= n - 2001 + 1;则闰年数=x/4-x/100+x/400;对于本题中的year来说,n= year-1;

另外本题在计算天数的时候应该看今天以前有多少天(不包括今天),这些天能构成几个月份,再把今天加到日号上。如果在计算天数的时候直接将今天算在内的话,就有可能造成构成整个月份的时候出错。当加上今天恰好构成整个月份的话,今天本来应该是上个月的最后一天,这样就变成了本月的第0天。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
usingnamespace std;

int monthday[2][13] =
{
{
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{
0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };

bool isleapyear(int y)
{
if (y %100==0&& y %400!=0)
returnfalse;
return y %4==0;
}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf(
"%d", &t);
while (t--)
{
int h, min, s, d, mon, y;
scanf(
"%d:%d:%d %d.%d.%d", &h, &min, &s, &d, &mon, &y);
longlong totalsec = h *3600+ min *60+ s;
totalsec
= totalsec *100000/ (3600*24);
printf(
"%lld:%lld:%lld", totalsec /10000, totalsec %10000/100,
totalsec
%100);
int totalday =0;
if (y !=2000)
totalday
=366+365* (y -1-2000) + (y -1-2000) /4- (y -1
-2000) /100+ (y -1-2000) /400;
bool leap = isleapyear(y);
for (int i =1; i < mon; i++)
totalday
+= monthday[leap][i];
totalday
+= d -1;
printf(
" %d.%d.%d\n", totalday %100+1, totalday %1000/100+1,
totalday
/1000);
}
return0;
}
原文地址:https://www.cnblogs.com/rainydays/p/2116066.html