1 题意
各时区的时间转换。
2分析
题不难写,但是细节上有点绕:题目给出的12小时的时间,以及测试数据所说的,存在12:40 pm,12:01am,前一个是凌晨的、后一个是中午的。那么选择什么样的思维来编码,对编码复杂度,有很大不同。
3学习
①模拟题,应该先在纸上,画一个树,画清各种情况(分类)的发展脉络(发展),再动手写就如虎添翼(因为还可能对于题目所给数据进行一些变化,看②),否则最后根据测试数据、删删减减,不但很可能还有不足还可能创造新的漏洞;换言之,对于模拟题,就应该用软件工程的方法来做。
②学会适当改造题目所给数据,(有可能题目所给的数据形式,是故意设坑,应该适当加以改造,稍微加一点思维复杂度,就能降低很大编码复杂度),将其转换成适合编码的数据,最后输出时看情况再是否转化。
example:
a.将12都改为0,就可以将12:01~11:59变成hour从0~11,min从0~59,然后就可以通过下面这一段来解决调整时间,
while(hour>11||hour<0){
if(hour<0){
hour+=12;
am=!am;
}
if(hour>11){
hour-=12;
am=!am;
}
}
b.但是,问题随之而来的,是通过Sample input 得知题目输入数据,是认为凌晨的12:40是前一天的pm表示,这与我们的目前的思路不太统一,我们将读入的像这种凌晨的12点的pm全变成am,同理,将中午12:01这种的am全变成pm,(即hour是12的am=1或0的、全部反转),这样就统一起来、也方便很多(如12:01 am. ,变为12:01 pm,12>11 so 12=12-12=0 am, 0->12 so 12:01 am),输出的时候,根据含义输出即可。
4代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
//注意,分钟补前导零,小时不补前导零。
char zone_name[32][10]={"UTC","GMT","BST","IST","WET","WEST","CET","CEST","EET","EEST","MSK","MSD","AST","ADT","NST","NDT","EST","EDT","CST","CDT","MST","MDT","PST","PDT","HST","AKST","AKDT","AEST","AEDT","ACST","ACDT","AWST"};
int zone_diff_hour[32]={0,0,1,1,0,1,1,2,2,3,3,4,-4,-3,-3,-2,-5,-4,-6,-5,-7,-6,-8,-7,-10,-9,-8,10,11,9,10,8};
int zone_diff_minn[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,-30,-30,0,0,0,0,0,0,0,0,0,0,0,0,0,30,30,0};
int hour,minn,am;
int zone1_num,zone2_num;
void Init(){
char str1[10],zone1[10],zone2[10],temp[10];
scanf("%s",str1);
if(strcmp(str1,"noon")==0){
hour=0;minn=0;am=0;
scanf("%s%s",zone1,zone2);
}
else if(strcmp(str1,"midnight")==0){
hour=0;minn=0;am=1;
scanf("%s%s",zone1,zone2);
}
else{
scanf("%s%s%s",temp,zone1,zone2);
if(temp[0]=='a'){
am=1;
}
else{
am=0;
}
int fenhao=0;
for(int i=0;i<strlen(str1);i++){
if(str1[i]==':'){
fenhao=i;
break;
}
}
if(fenhao==1){
hour=(str1[0]-'0');
minn=(str1[2]-'0')*10+(str1[3]-'0');
}
else if(fenhao==2){
hour=(str1[0]-'0')*10+(str1[1]-'0');
minn=(str1[3]-'0')*10+(str1[4]-'0');
}
if(hour==12){
am=!am;
}
}
for(int i=0;i<32;i++){
if(strcmp(zone1,zone_name[i])==0){
zone1_num=i;
}
if(strcmp(zone2,zone_name[i])==0){
zone2_num=i;
}
}
}
void Print(){
int diff_hour=0;
int diff_minn=0;
int old_hour=hour;
diff_hour=(-1)*(zone_diff_hour[zone1_num])+(zone_diff_hour[zone2_num]);
diff_minn=(-1)*(zone_diff_minn[zone1_num])+(zone_diff_minn[zone2_num]);
minn+=diff_minn;
while(minn>59||minn<0){
if(minn<0){
minn+=60;
hour--;
}
if(minn>59){
minn-=60;
hour++;
}
}
hour+=diff_hour;
while(hour>11||hour<0){
if(hour<0){
hour+=12;
am=!am;
}
if(hour>11){
hour-=12;
am=!am;
}
}
if(0==hour){
hour=12;
}
if(hour==12&&minn==00){
if(am) cout<<"midnight"<<endl;
else cout<<"noon"<<endl;
}
else{
printf("%d:%02d ",hour,minn);
if(am) cout<<"a.m."<<endl;
else cout<<"p.m."<<endl;
}
}
int main()
{
//freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
while(n--){
Init();
Print();
}
}