The World
题目
The World can indicate world travel, particularly on a large scale. You may be lucky enough to be embarking on a six-month overseas trip, or are working, studying or living overseas for an extended period of time. Similarly, this card reinforces Universal understanding and global awareness, and you will have a new appreciation for people and cultures from across the world.
Across the world there are various time zones, leading to time differences. Here you are informed of several famous capitals and their corresponding time zones.
Beijing - China - UTC + 8 (China Standard Time)
Washington - United States - UTC - 5 (Eastern Standard Time)
London - United Kingdom - UTC (Greenwich Mean Time)
Moscow - Russia - UTC + 3 (Moscow Time)
Given the local time of a city, you are expected to calculate the date and local time of another specific city among the above capitals.
Input
The first line of input contains a single integer T ≤ 1000 indicating the number of testcases.
Each testcase consists of three lines. The first line is in the form of “hour:minute AM/PM” (1 ≤ hour ≤ 12, 00 ≤ minute ≤ 59) indicating the local time. Next two lines contain two strings s1, s2. s1 is the name of city corresponding to the given time, while s2 indicates the city you are expected to calculate the local time.
Output
For each testcase, begin with “Case i:”, where i indicate the case number, and then output a single line in the following format“Yesterday/Today/Tomorrow hour:minute AM/PM”, separated by spaces. The first word describes the corresponding date.
Sample
Input
2
12:00 AM
London
Moscow
4:00 PM
London
Beijing
Output
Case 1: Today 3:00 AM
Case 2: Tomorrow 12:00 AM
解题思路
一开始对这个AM和PM的时间范围有点混乱,以为12:00 AM是中午十二点,12:00 PM是晚上十二点。弄清楚之后,发现AM指一天中的午前(12点之前),即0:00:00~11:59:59,PM指一天中的午后(12点之后),即(12:00:00 ~ 23:59:59),也就是从12,1,2,3……这样算,就好做多了。
AC
#include <iostream>
#include<cstdio>
#include <map>
#include <string>
using namespace std;
map<string, int> m;
int main()
{
m["Beijing"] = 8;
m["Washington"] = -5;
m["London"] = 0;
m["Moscow"] = 3;
int t, h, min, time;
string ap, s1, s2;
cin >> t;
for(int i = 1; i <= t ; i ++ )
{
scanf("%d:%d",&h,&min);
cin >> ap >> s1 >> s2;
if(ap == "AM")
time = h;
else time = h + 12;
if(h == 12)
time = time - 12;
time = time - m[s1] + m[s2];
cout<<"Case "<<i<<": ";
if (time >= 24)
cout<<"Tomorrow ";
else if(time < 0)
cout<<"Yesterday ";
else
cout<<"Today ";
h = (time + 24) % 24;
if(h >= 12)
printf("%d:%02d PM\n",h==12?12:h-12,min);
else
printf("%d:%02d AM\n",h==0?12:h,min);
}
return 0;
}