目錄
題目1 : 閏秒
時間限制:10000ms
單點時限:1000ms
內存限制:256MB
描述
計算機系統中使用的UTC時間基於原子鐘,這種計時方式同“地球自轉一週是24小時”的計時方式有微小的偏差。爲了彌補這種偏差,我們偶爾需要增加一個“閏秒”。
最近的一次閏秒增加發生在UTC時間2016年的最後一天。我們在2016年12月31日23時59分59秒和2017年1月1日0時0分0秒之間增加了這樣一秒:2016年12月31日23時59分60秒,記作2016-12-31 23:59:60。
目前一共增加了27次閏秒,具體添加的時間見下表:
給出兩個時間,請你判斷在考慮閏秒的情況下,這兩個時間間隔多少秒。
輸入
兩個時間各佔一行,格式是yyyy-MM-dd HH:mm:ss,範圍在1970-01-01 00:00:00至2017-03-12 23:59:59之間。保證第一個時間不晚於第二個時間。
輸出
兩個時間間隔多少秒。
樣例輸入
2016-12-31 23:59:59
2017-01-01 00:00:00
樣例輸出
2
題意分析:
1.題是什麼?
給你兩個準確到秒的時刻,讓你輸出這兩個時刻之間相差多少秒,要考慮閏秒與閏年.
2.思路
自定義一個時間類,完成基礎的配置後寫一個parse方法計算與標準時間1970-01-01 00:00:00的秒差,兩個時刻的秒差相減即爲答案,關於閏秒的處理我是做出一個天生有序的閏秒時刻數組,獨立計算閏秒的.
ac代碼:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
bool isleapyear(int year){
if((year%4==0&&year%100!=0)||(year%400==0)) return true;
return false;
}
const int leapyear[12]={1972,1976,1980,1984,1988,1992,1996,2000,2004,2008,2012,2016};//題目時間範圍內的閏年
const int month_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};//不考慮閏年,每月的天數
const int qzh_month_day[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};//month_day的前綴和
class date{
private:
int year,month,day,hour,minute,second;
public:
date();
date(string tdate);
int parse();
friend ostream& operator<<(ostream& output,const date &D);
friend istream& operator>>(istream& input,date &D);
friend bool operator<(const date &a,const date &b){
long long timea=(long long)a.year*1e10+(long long)a.month*1e8+a.day*1e6+a.hour*1e4+a.minute*1e2+a.second;
long long timeb=(long long)b.year*1e10+(long long)b.month*1e8+b.day*1e6+b.hour*1e4+b.minute*1e2+b.second;
return timea<timeb;
}
};
date::date(){
year=month=day=hour=minute=second=0;
}
date::date(string tdate){
year=atoi(tdate.substr(0,4).c_str());
month=atoi(tdate.substr(5,2).c_str());
day=atoi(tdate.substr(8,2).c_str());
hour=atoi(tdate.substr(11,2).c_str());
minute=atoi(tdate.substr(14,2).c_str());
second=atoi(tdate.substr(17,2).c_str());
}
const date leapseconds[27]={
date("1972-06-30 23:59:60"),date("1972-12-31 23:59:60"),
date("1973-12-31 23:59:60"),date("1974-12-31 23:59:60"),date("1975-12-31 23:59:60"),date("1976-12-31 23:59:60"),date("1977-12-31 23:59:60"),
date("1978-12-31 23:59:60"),date("1979-12-31 23:59:60"),date("1981-06-30 23:59:60"),date("1982-06-30 23:59:60"),date("1983-06-30 23:59:60"),
date("1985-06-30 23:59:60"),date("1987-12-31 23:59:60"),date("1989-12-31 23:59:60"),date("1990-12-31 23:59:60"),date("1992-06-30 23:59:60"),
date("1993-06-30 23:59:60"),date("1994-06-30 23:59:60"),date("1995-12-31 23:59:60"),date("1997-06-30 23:59:60"),date("1998-12-31 23:59:60"),
date("2005-12-31 23:59:60"),date("2008-12-31 23:59:60"),date("2012-06-30 23:59:60"),date("2015-06-30 23:59:60"),date("2016-12-31 23:59:60")};
int date::parse(){//返回此時間與1970-01-01 00:00:00的秒差
int ans=0;
//只考慮閏年的秒差
ans+=(year-1970)*365*24*3600;
for(int i=0;i<12;i++) if(leapyear[i]<year) ans+=24*3600;
if(isleapyear(year)&&month>2) ans+=24*3600;
ans+=qzh_month_day[month-1]*24*3600;
ans+=(day-1)*24*3600;
ans+=hour*3600+minute*60+second;
//閏秒增加的秒差
for(int i=0;i<27;i++) if(leapseconds[i]<*this) ans+=1;
return ans;
}
ostream& operator<<(ostream& output,const date &D){
printf("%04d-%02d-%02d %02d:%02d:%02d",D.year,D.month,D.day,D.hour,D.minute,D.second);
return output;
}
istream& operator>>(istream& input,date &D){
scanf("%d-%d-%d %d:%d:%d",&D.year,&D.month,&D.day,&D.hour,&D.minute,&D.second);
return input;
}
void solve(){
date a,b;
cin>>a>>b;
cout<<b.parse()-a.parse()<<endl;
}
int main(){
solve();
return 0;
}