POJ2966时区时间转换

Description
直到19世纪,时间校准是一个纯粹的地方现象。每一个村庄当太阳升到最高点的时候把他们的时钟调到中午12点。一个钟表制造商人家或者村里主表的时间被认为是官方时间,市民们把自家的钟表和这个时间对齐。每周一些热心的市民会带着时间标准的表,游走大街小巷为其他市民对表。在城市之间旅游的话,在到达新地方的时候需要把怀表校准。但是,当铁路投入使用之后,越来越多的人频繁地长距离地往来,时间变得越来越重要。在铁路的早期,时刻表非常让人迷惑,每一个所谓的停靠时间都是基于停靠地点的当地时间。时间的标准化对于铁路的高效运营变得非常重要。 
在1878年,加拿大人Sir Sanford Fleming 提议使用一个全球的时区(这个建议被采纳,并衍生了今天我们所使用的全球时区的概念),他建议把世界分成24个时区,每一个跨越15度经线(因为地球的经度360度,划分成24块后,一块为15度)。Sir Sanford Fleming的方法解决了一个全球性的时间混乱的问题。 
美国铁路公司于1883年11月18日使用了Fleming 提议的时间方式。1884年一个国际子午线会议在华盛顿召开,他的目的是选择一个合适的本初子午线。大会最终选定了格林威治为标准的0度。尽管时区被确定了下来,但是各个国家并没有立刻更改他们的时间规范,在美国,尽管到1895年已经有很多州开始使用标准时区时间,国会直到1918年才强制使用会议制定的时间规范。 
今天各个国家使用的是一个Fleming时区规范的一个变种,中国一共跨越了5个时区,但是使用了一个统一的时间规范,比Coordinated Universal Time(UTC,格林威制时间)早8个小时。俄罗斯也拥护这个时区规范,尽管整个国家使用的时间和标准时区提前了1个小时。澳大利亚使用3个时区,其中主时区提前于他按Fleming规范的时区半小时。很多中东国家也使用了半时时区(即不是按照Fleming的24个整数时区)。 
因为时区是对经度进行划分,在南极或者北极工作的科学家直接使用了UTC时间,否则南极大陆将被分解成24个时区。 
时区的转化表如下: 
UTC Coordinated Universal Time 
GMT Greenwich Mean Time, 定义为 UTC 
BST British Summer Time, 定义为 UTC+1 hour 
IST Irish Summer Time, 定义为 UTC+1 hour 
WET Western Europe Time, 定义为 UTC 
WEST Western Europe Summer Time, 定义为 UTC+1 hour 
CET Central Europe Time, 定义为 UTC+1 
CEST Central Europe Summer Time, 定义为 UTC+2 
EET Eastern Europe Time, 定义为 UTC+2 
EEST Eastern Europe Summer Time, 定义为 UTC+3 
MSK Moscow Time, 定义为 UTC+3 
MSD Moscow Summer Time, 定义为 UTC+4 
AST Atlantic Standard Time, 定义为 UTC-4 hours 
ADT Atlantic Daylight Time, 定义为 UTC-3 hours 
NST Newfoundland Standard Time, 定义为 UTC-3.5 hours 
NDT Newfoundland Daylight Time, 定义为 UTC-2.5 hours 
EST Eastern Standard Time, 定义为 UTC-5 hours 
EDT Eastern Daylight Saving Time, 定义为 UTC-4 hours 
CST Central Standard Time, 定义为 UTC-6 hours 
CDT Central Daylight Saving Time, 定义为 UTC-5 hours 
MST Mountain Standard Time, 定义为 UTC-7 hours 
MDT Mountain Daylight Saving Time, 定义为 UTC-6 hours 
PST Pacific Standard Time, 定义为 UTC-8 hours 
PDT Pacific Daylight Saving Time, 定义为 UTC-7 hours 
HST Hawaiian Standard Time, 定义为 UTC-10 hours 
AKST Alaska Standard Time, 定义为 UTC-9 hours 
AKDT Alaska Standard Daylight Saving Time, 定义为 UTC-8 hours 
AEST Australian Eastern Standard Time, 定义为 UTC+10 hours 
AEDT Australian Eastern Daylight Time, 定义为 UTC+11 hours 
ACST Australian Central Standard Time, 定义为 UTC+9.5 hours 
ACDT Australian Central Daylight Time, 定义为 UTC+10.5 hours 
AWST Australian Western Standard Time, 定义为 UTC+8 hours 
下面给出了一些时间,请在不同时区之间进行转化。
Input
输入的第一行包含了一个整数N,表示有N组测试数据。接下来N行,每一行包括一个时间和两个时区的缩写,它们之间用空格隔开。时间由标准的a.m./p.m给出。midnight表示晚上12点(12:00 a.m.),noon表示中午12点(12:00 p.m.)。
Output
假设输入行给出的时间是在第一个时区中的标准时间,要求输出这个时间在第二个时区中的标准时间。
Sample Input
4
noon HST CEST 
11:29 a.m. EST GMT
6:01 p.m. CST UTC
12:40 p.m. ADT MSK
Sample Output
midnight
4:29 p.m.
12:01 a.m.

6:40 p.m.

一开始代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
	char time[32][10]={"UTC","GMT","BST","IST","WET","WEST","CET","CEST","EET","EEST","MSK","MSD","AST","ADT","NST","NDT","EST","EDT","CST","CDT","MST","MDT","PST","PDT","HST","AKST","AKDT","AEST","AEDT","ACST","ACDT","AWST"};
	float tioff[32]={ 0, 0, 1, 1, 0, 1, 1, 2, 2, 3, 
                   3, 4, -4, -3, -3.5, -2.5, -5, -4, -6, -5, 
                   -7, -6, -8, -7, -10, -9, -8, 10, 11, 9.5, 
                   10.5, 8};
	int dif(char*s1,char*s2){
	int i,j;
	for(j=0;j<32;j++)
	{if(strcmp(s1,time[j])==0) break;
	  }
	for(i=0;i<32;i++)
	{if(strcmp(s2,time[i])==0) break;
	  }
	return (int)((tioff[j]-tioff[i])*60);}
int main(){
	int n;
	int hour;
	int min;
	char when[15];
	char a[5];
	char first[10];
	char sed[10];
	int i,j;
	int total;
	scanf("%d",&n);	
	getchar();
	for(i=0;i<n;i++)
	{ scanf("%s",when);
	switch(when[0]){
	case 'n':{hour=12;min=0;break;}
	case 'm':{hour=0;min=0;break;}
	default:sscanf(when,"%d:%d",&hour,&min);
	hour%=12;//in case 12:xx p.m.
	scanf("%s",a);
	if(a[0]=='p')hour+=12;}
	scanf("%s %s",first,sed);
	total=hour*60+min+dif(first,sed);
	if(total<0)   total+=24*60;
	if(total>=24*60)total-=24*60;
	hour=total/60;
	min=total%60;
	switch(hour){
	case 0:{if(min==0){printf("midnight\n");}
		   else {printf("12:%2d a.m.\n",min);}
		   break;}
	case 12:{if(min==0){printf("noon\n");}
			else {printf("12:%2d p.m.\n",min);}
		   break;}
	default:if(hour>12){printf("%d:%2d p.m.\n",hour-12,min);}
		   else {printf("%d:%2d a.m.\n",hour,min);}
	}
	}
	
	return 0;}
提交后WC,找了好久,终于找到原因:

原因1:在求两个时区时差时的函数,最后一步(int)((tioff[i]-tioff[j])*60),实际上应该反过来,即要求的时区减去已知的时区;

原因2:还是输出问题,题目要求输出xx:0x的格式,printf("%d:%02d a.m.\n",hour,min)只会产生xx: x的格式,没有0:,改正:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
	char time[32][10]={"UTC","GMT","BST","IST","WET","WEST","CET","CEST","EET","EEST","MSK","MSD","AST","ADT","NST","NDT","EST","EDT","CST","CDT","MST","MDT","PST","PDT","HST","AKST","AKDT","AEST","AEDT","ACST","ACDT","AWST"};
	float tioff[32]={ 0, 0, 1, 1, 0, 1, 1, 2, 2, 3, 
                   3, 4, -4, -3, -3.5, -2.5, -5, -4, -6, -5, 
                   -7, -6, -8, -7, -10, -9, -8, 10, 11, 9.5, 
                   10.5, 8};
	int dif(char*s1,char*s2){
	int i,j;
	for(j=0;j<32;j++)
	{if(strcmp(s1,time[j])==0) break;
	  }
	for(i=0;i<32;i++)
	{if(strcmp(s2,time[i])==0) break;
	  }
	return (int)((tioff[i]-tioff[j])*60);}
int main(){
	int n;
	int hour;
	int min;
	char when[15];
	char a[5];
	char first[10];
	char sed[10];
	int i,j;
	int total;
	scanf("%d",&n);	
	getchar();
	for(i=0;i<n;i++)
	{ scanf("%s",when);
	switch(when[0]){
	case 'n':{hour=12;min=0;break;}
	case 'm':{hour=0;min=0;break;}
	default:sscanf(when,"%d:%d",&hour,&min);
	hour%=12;//in case 12:xx p.m.
	scanf("%s",a);
	if(a[0]=='p')hour+=12;}
	scanf("%s %s",first,sed);
	total=hour*60+min+dif(first,sed);
	if(total<0)   total+=24*60;
	if(total>=24*60)total-=24*60;
	hour=total/60;
	min=total%60;
	switch(hour){
	case 0:{if(min==0){printf("midnight\n");}
		   else {printf("12:%02d a.m.\n",min);}
		   break;}
	case 12:{if(min==0){printf("noon\n");}
			else {printf("12:%02d p.m.\n",min);}
		   break;}
	default:if(hour>12){printf("%d:%02d p.m.\n",hour-12,min);}
		   else {printf("%d:%02d a.m.\n",hour,min);}
	}
	}
	
	return 0;}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值