hiho235 闰秒

目录

题目1 : 闰秒

题意分析:

1.题是什么?

2.思路

ac代码:


题目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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章