2015年9月29日 星期二

[UVA] 10371 Time Zones

題意:
給時間, 字串A, 字串B
字串A B可以根據他給的表轉成數字A和數字B
輸出時間 + (數字B - 數字A)
--------------------------------------------------

我的作法:
可以把時間變成24小時制
12:00 a.m. 變成 0:00
1:00 p.m. 變成 13:00
在做時間相加或相減可以先把時:分都先換成
例如:
1306分 + 5.5
136分 = 13*60+6 = 0786
5.5時 = 5.5*60 = 330分,
0786分+330分 = 01116
再換算回來 = (1116/60)時(1116%60)分 = 1836

最後要輸出時只要判斷是否有大於12時,就知道要輸出pm還是am了!
但要注意的是! 0:05 a.m.要換成 12:05 a.m.喔
--------------------------------------------------


/* 20150929
 * hanting
 * UVa 10371 Time Zones
 * C++
 */
#include <iostream>
#include <map>
#include <sstream>
#include <iomanip> //setw,setfill
#include <cstring> //strtok
#include <cstdlib> //atoi
using namespace std;
map<string,double> Map;
void init()
{
    Map["UTC"]=0;
    Map["GMT"]=0;
    Map["BST"]=1;
    Map["IST"]=1;
    Map["WET"]=0;
    Map["WEST"]=1;
    Map["CET"]=1;
    Map["CEST"]=2;
    Map["EET"]=2;
    Map["EEST"]=3;
    Map["MSK"]=3;
    Map["MSD"]=4;
    Map["AST"]=-4;
    Map["ADT"]=-3;
    Map["NST"]=-3.5;
    Map["NDT"]=-2.5;
    Map["EST"]=-5;
    Map["EDT"]=-4;
    Map["CST"]=-6;
    Map["CDT"]=-5;
    Map["MST"]=-7;
    Map["MDT"]=-6;
    Map["PST"]=-8;
    Map["PDT"]=-7;
    Map["HST"]=-10;
    Map["AKST"]=-9;
    Map["AKDT"]=-8;
    Map["AEST"]=10;
    Map["AEDT"]=11;
    Map["ACST"]=9.5;
    Map["ACDT"]=10.5;
    Map["AWST"]=8;
}
struct Time
{
    int hour,minute;
    Time():hour(0),minute(0){}
    void operator+=(int a)
    {
        hour+=a;
        hour=(hour+24)%24;
    }
    Time operator+(double d)
    {
        Time result;
        int tmp=minute+(hour+24)*60+d*60;
        result.minute=(tmp)%60;
        result.hour=(tmp/60)%24;
        return result;
    }
    friend istream& operator>>(istream& in,Time &t)
    {
        string cur;
        in>>cur;
        if(cur=="noon")
        {
            t.hour=12;
            t.minute=0;
        }
        else if(cur=="midnight")
        {
            t.hour=0;
            t.minute=0;
        }
        else
        {
            //將12:03分成12和3
            char *pch=strtok((char*)cur.c_str(),":");
            t.hour=atoi(pch);
            pch=strtok(NULL,":");
            t.minute=atoi(pch);
        }
        return in;
    }
    friend ostream& operator<<(ostream& out,Time &t)
    {
        if(t.hour==12 and t.minute==0) out<<"noon";
        else if(t.hour==0 and t.minute==0) out<<"midnight";
        else
        {
            if(t.hour>=12) out<<(t.hour==12 ? 12:t.hour-12)<<":"<<setw(2)<<setfill('0')<<t.minute<<" p.m.";
            else out<<(t.hour==0 ? 12:t.hour)<<":"<<setw(2)<<setfill('0')<<t.minute<<" a.m.";
        }
        return out;
    }
};
int main()
{
    init();
    int caseN;
    cin>>caseN;
    cin.get();
    while(caseN--)
    {
        string str;
        getline(cin,str);
        stringstream sin(str);
        Time cur;
        string ampm;
        string timeZone[2];
        sin>>cur;
        if(cur.minute==0 and (cur.hour==0 or cur.hour==12))//noon,midnight
            sin>>timeZone[0]>>timeZone[1];
        else
        {
            sin>>ampm>>timeZone[0]>>timeZone[1];
            if(ampm=="p.m." and cur.hour!=12) cur+=12;
            else if(ampm=="a.m." and cur.hour==12) cur.hour=0;
        }
        double d=(Map[timeZone[1] ]-Map[timeZone[0] ]);
        Time ans=cur+d;
        cout<<ans<<endl;
    }
    return 0;
}

沒有留言:

張貼留言