POJ 2966 时区转换 解题报告
POJ 2966 时区转换 解题报告
编号:2966
考查点:日期和时间处理
思路:还是找到公共点,都把时间转换为分钟,最后通过分钟来计算目标时区的时间表示
提交情况:这道题是我用时间最多的,历经三天,代码重写了无数遍,不容易..
Source Code:
//POJ Grids 2966
#include <string>
#include<iostream>
using namespace std;
float Time(const char* time)
{
if (strcmp(time,"UTC")==0||strcmp(time,"GMT")==0||strcmp(time,"WET")==0)
{
return 0;
}
if (strcmp(time,"BST")==0||strcmp(time,"IST")==0||strcmp(time,"WEST")==0||strcmp(time,"CET")==0)
{
return 1;
}
if (strcmp(time,"CEST")==0||strcmp(time,"EET")==0)
{
return 2;
}
if (strcmp(time,"EEST")==0||strcmp(time,"MSK")==0)
{
return 3;
}
if (strcmp(time,"MSD")==0)
{
return 4;
}
if (strcmp(time,"AST")==0||strcmp(time,"EDT")==0)
{
return -4;
}
if (strcmp(time,"ADT")==0)
{
return -3;
}
if (strcmp(time,"NST")==0)
{
return -3.5;
}
if (strcmp(time,"NDT")==0)
{
return -2.5;
}
if (strcmp(time,"EST")==0||strcmp(time,"CDT")==0)
{
return -5;
}
if (strcmp(time,"CST")==0||strcmp(time,"MDT")==0)
{
return -6;
}
if (strcmp(time,"MST")==0||strcmp(time,"PDT")==0)
{
return -7;
}
if (strcmp(time,"PST")==0||strcmp(time,"AKDT")==0)
{
return -8;
}
if (strcmp(time,"AKST")==0)
{
return -9;
}
if (strcmp(time,"HST")==0)
{
return -10;
}
if (strcmp(time,"AWST")==0)
{
return 8;
}
if (strcmp(time,"AEST")==0)
{
return 10;
}
if (strcmp(time,"AEDT")==0)
{
return 11;
}
if (strcmp(time,"ACDT")==0)
{
return 10.5;
}
if (strcmp(time,"ACST")==0)
{
return 9.5;
}
cout<<"Error"<<endl;
return -1;
}
int main()
{
int n;
cin>>n;
while (n--)
{
string first;
string second;
cin>>first;
int ho,mi;
ho = mi = 0;
if (first=="noon"||first=="midnight")
{
if (first=="noon")
{
ho = 12;
}
}
else
{
sscanf(first.c_str(),"%d:%d",&ho,&mi);
cin>>second;
if (second=="p.m.")
{
ho %= 12;
ho += 12;
}
if (second=="a.m.")
{
if (ho==12)
{
ho = 0;
}
}
}
string zone,ztwo;
cin>>zone>>ztwo;
int minutes = ho*60+mi-Time(zone.c_str())*60+Time(ztwo.c_str())*60;
minutes = (minutes+1440)%1440;
string time = "a.m.";
if (minutes==0||minutes==720)
{
if (minutes)
{
cout<<"noon"<<endl;
}
else
cout<<"midnight"<<endl;
}
else
{
ho = minutes/60;
mi = minutes%60;
if (ho==0)
{
ho = 12;
}
else if (ho>=12)
{
time = "p.m.";
if(ho!=12)
ho -= 12;
}
printf("%d:%02d %s\n",ho,mi,time.c_str());
}
}
return 0;
}
总结:这次浪费时间多重要是POJ显示Wrong Answer(The test data is not available),其实还是WA的意思,我还以为是POJ的问题呢,就一直把这道题半死不活的放那,今天在别人的帮助下,学会了用string代替char*,用sscanf函数得到小时和分钟,努力了很长时间,终于AC。
By Ns517
Time 09.01.26