HDU 6556 The World

題意:已知四個城市的時區,給你一個城市的時間,轉換成另一個城市的時間。

坑點:12:00 AM是晚上0點,12:00 PM是中午12點

題解:所有時間轉換題全都用24小時制!!!
方便快捷,一開始用的12小時制算的頭暈,特殊情況太多,換成24小時制直接把這題秒了。

分鐘是不會動的,我們只考慮小時。
當前是 12:00 AM,轉換成24小時制應該是0點。
當前是 12:00 PM,轉換成24小時應該不變。
當前是 PM 且不是 12 點,則 小時+12 爲24小時制。

24小時制小時只能爲0~23,不要出現24
(避免今天24點這種表達)

轉換之後,如果 h<0h<0 ,說明是昨天;如果 h>23h>23 ,說明是明天。
再把24小時轉換回題中詭異的12小時制。
h<12h<12 說明是上午,如果是 0 點,輸出12;其他不變。
h>=12h>=12 說明是下午,12點直接輸出,否則輸出 h12h-12

#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string,int>zone;
int main()
{
    zone["Beijing"]=8;
    zone["Washington"]=-5;
    zone["London"]=0;
    zone["Moscow"]=3;
    int t;
    cin>>t;
    for(int it=1;it<=t;it++)
    {
        string s1,s2,s3,s4,tmpm="";
        cin>>s1>>s2>>s3>>s4;
        int tmph=0,bef=0;
        for(int i=0;i<s1.size();i++)
        {
            if(s1[i]==':') 
            {
                bef=1;
                continue;
            }
            if(bef==0) tmph=tmph*10+s1[i]-'0';
            else tmpm+=s1[i];
        }
        if(tmph==12&&s2=="AM") tmph=0;
        if(s2=="PM"&&tmph!=12) tmph+=12;//轉換成24小時制,注意中午12:00不要+12了
        int ansh=tmph+zone[s4]-zone[s3];
        string s5="Today";
        if(ansh<0) 
        {
            ansh+=24;
            s5="Yesterday";
        }
        else if(ansh>23)
        {
            ansh-=24;
            s5="Tomorrow";
        }
        s2=(ansh<12)?"AM":"PM";
        if(ansh==0) ansh=12;
        if(ansh>12) ansh-=12;
        // while((ansh<0)||(ansh>12))//0-13=-13,最小是-13,要循環兩遍;
        // {                         //應該是ansh<0而不是ansh<1
        //     if(ansh<0) 
        //     {
        //         ansh+=12;
        //         if(s2=="AM") 
        //         {
        //             s5="Yesterday";//s5統一末尾不加空格
        //             s2="PM";
        //         }
        //         else 
        //         {
        //             // s5="Today";//-13會把第一次的Yesterday改成Today
        //             s2="AM";
        //         }
        //     }
        //     else 
        //     {
        //         ansh-=12;
        //         if(s2=="AM") s2="PM";
        //         else 
        //         {
        //             s5="Tomorrow";
        //             s2="AM";
        //         }
        //     }
        // }
        // if(ansh==0&&s2=="AM") ansh=12;
        // if(ansh==12&&s2=="AM") s2="PM";//今天的中午十二點要做出改變
        // // if(ansh==12&&s2=="PM") 當1,AM -13時,應該輸出12:00 PM,在這裏會被改遍
        // // {
        // //     s5="Tomorrow";
        // //     s2="AM";
        // // }
        // if()
        cout<<"Case "<<it<<": ";
        cout<<s5<<" "<<ansh<<":"<<tmpm<<" "<<s2<<endl;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章