POJ 2351 - Time Zones

http://poj.org/problem?id=2351

概要

問題文で与えられたタイムゾーンに従って,あるタイムゾーンでの時間が別のタイムゾーンだと何時になるか答える.

制約

とくになし

解法

やるだけ. 24時間表記で 12:00 が「noon」であり,0:00 が「midnight」であることに注意. また,12:01 が「12:01 p.m.」であったり,0:01 が「12:01 a.m.」であったりすることにも注意.

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <sstream>
  4 #include <vector>
  5 #include <map>
  6 using namespace std;
  7 
  8 int parse(const string& s)
  9 {
 10   int h = 0;
 11   string::const_iterator it = s.begin();
 12   while (*it != ':') {
 13     h = 10*h + (*it-'0');
 14     ++it;
 15   }
 16   ++it;
 17   int m = 0;
 18   while (it != s.end()) {
 19     m = 10*m + (*it-'0');
 20     ++it;
 21   }
 22   if (h == 12) {
 23     h = 0;
 24   }
 25   return h*60+m;
 26 }
 27 
 28 int main()
 29 {
 30   map<string,int> tz;
 31   tz["UTC"] = 0;
 32   tz["GMT"] = 0;
 33   tz["BST"] = +1*60;
 34   tz["IST"] = +1*60;
 35   tz["WET"] = 0;
 36   tz["WEST"] = +1*60;
 37   tz["CET"] = +1*60;
 38   tz["CEST"] = +2*60;
 39   tz["EET"] = +2*60;
 40   tz["EEST"] = +3*60;
 41   tz["MSK"] = +3*60;
 42   tz["MSD"] = +4*60;
 43   tz["AST"] = -4*60;
 44   tz["ADT"] = -3*60;
 45   tz["NST"] = -3*60-30;
 46   tz["NDT"] = -2*60-30;
 47   tz["EST"] = -5*60;
 48   tz["EDT"] = -4*60;
 49   tz["CST"] = -6*60;
 50   tz["CDT"] = -5*60;
 51   tz["MST"] = -7*60;
 52   tz["MDT"] = -6*60;
 53   tz["PST"] = -8*60;
 54   tz["PDT"] = -7*60;
 55   tz["HST"] = -10*60;
 56   tz["AKST"] = -9*60;
 57   tz["AKDT"] = -8*60;
 58   tz["AEST"] = +10*60;
 59   tz["AEDT"] = +11*60;
 60   tz["ACST"] = +9*60+30;
 61   tz["ACDT"] = +10*60+30;
 62   tz["AWST"] = +8*60;
 63 
 64   string line;
 65   getline(cin, line);
 66   int N;
 67   {
 68     istringstream iss(line);
 69     iss >> N;
 70   }
 71   while (N-- > 0) {
 72     getline(cin, line);
 73     istringstream iss(line);
 74     vector<string> tks;
 75     for (string s; iss >> s; tks.push_back(s));
 76     int t;
 77     int from, to;
 78     if (tks.size() == 4) {
 79       t = parse(tks[0]);
 80       if (tks[1] == "p.m.") {
 81         t += 12*60;
 82       }
 83       if (!tz.count(tks[2]) || !tz.count(tks[3])) {
 84         throw "honi";
 85       }
 86       from = tz[tks[2]];
 87       to = tz[tks[3]];
 88     } else {
 89       if (tks[0] == "noon") {
 90         t = 12*60;
 91       } else {
 92         t = 0;
 93       }
 94       if (!tz.count(tks[1]) || !tz.count(tks[2])) {
 95         throw "honi";
 96       }
 97       from = tz[tks[1]];
 98       to = tz[tks[2]];
 99     }
100     t = t - from + to;
101     t = (t+24*60)%(24*60);
102     if (t == 0) {
103       cout << "midnight" << endl;
104     } else if (t == 12*60) {
105       cout << "noon" << endl;
106     } else if (t < 12*60) {
107       int h = t/60;
108       if (h == 0) {
109         h = 12;
110       }
111       printf("%d:%02d a.m.\n", h, t%60);
112     } else {
113       t -= 12*60;
114       int h = t/60;
115       if (h == 0) {
116         h = 12;
117       }
118       printf("%d:%02d p.m.\n", h, t%60);
119     }
120   }
121   return 0;
122 }
poj/2351.cc