1. 程式人生 > >Bailian2966 時區轉換【時區計算】

Bailian2966 時區轉換【時區計算】

2966:時區轉換
總時間限制: 1000ms 記憶體限制: 65536kB
描述
直到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
下面給出了一些時間,請在不同時區之間進行轉化。
輸入


輸入的第一行包含了一個整數N,表示有N組測試資料。接下來N行,每一行包括一個時間和兩個時區的縮寫,它們之間用空格隔開。時間由標準的a.m./p.m給出。midnight表示晚上12點(12:00 a.m.),noon表示中午12點(12:00 p.m.)。
輸出
假設輸入行給出的時間是在第一個時區中的標準時間,要求輸出這個時間在第二個時區中的標準時間。
樣例輸入
4
noon HST CEST
11:29 a.m. EST GMT
6:01 p.m. CST UTC
12:40 p.m. ADT MSK
樣例輸出
midnight
4:29 p.m.
12:01 a.m.
6:40 p.m.

問題連結Bailian2966 時區轉換


問題簡述:(略)
問題分析
    本題與參考連結的題是同一題,程式碼可以直接AC。
程式說明:(略)
參考連結POJ2351 ZOJ1916 UVA10371 Time Zones【時區計算】
題記:(略)

AC的C語言程式如下:

/* POJ2351 ZOJ1916 UVA10371 Time Zones */

#include <stdio.h>
#include <string.h>

char *zone[]={"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 time[]={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 diff(char *zone1, char *zone2)
{
    int i, j;
    for(i = 0; strcmp(zone[i], zone1); i++);
    for(j = 0; strcmp(zone[j], zone2); j++);
    return (int)((time[i] - time[j]) * 60);
}

int main(void)
{
    int t;
    scanf("%d", &t);
    while(t--) {
        char buf[9];
        int hours, minutes;

        scanf("%s", buf);
        if(buf[0] == 'n')              /* noon */
            hours = 12, minutes = 0;
        else if(buf[0] == 'm')     /* midnight */
            hours = 0, minutes = 0;
        else {
            sscanf(buf, "%d:%d", &hours, &minutes);
            hours %= 12;
            scanf("%s", buf);      /* a.m. or p.m. */
            if(buf[0] == 'p')
                hours += 12;
        }

        char tz1[5], tz2[5];
        scanf("%s%s", tz1, tz2);
        int newtime = hours * 60 + minutes + diff(tz2, tz1);
        if(newtime < 0) newtime += 1440;
        newtime %= 1440;

        if(newtime == 0)
            printf("midnight\n");
        else if(newtime == 720)
            printf("noon\n");
        else {
            hours = newtime / 60;
            minutes = newtime % 60;
            if(hours == 0)
                printf("12:%02d a.m.\n", minutes);
            else if(hours < 12)
                printf("%d:%02d a.m.\n", hours, minutes);
            else if(hours == 12)
                printf("12:%02d p.m.\n", minutes);
            else
                printf("%d:%02d p.m.\n", hours % 12, minutes);
        }
    }

    return 0;
}