题意:已知四个城市的时区,给你一个城市的时间,转换成另一个城市的时间。
坑点: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点这种表达)
转换之后,如果
,说明是昨天;如果
,说明是明天。
再把24小时转换回题中诡异的12小时制。
说明是上午,如果是 0 点,输出12;其他不变。
说明是下午,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;
}
}