6:40 p.m.
一开始代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> char time[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"}; float tioff[32]={ 0, 0, 1, 1, 0, 1, 1, 2, 2, 3, 3, 4, -4, -3, -3.5, -2.5, -5, -4, -6, -5, -7, -6, -8, -7, -10, -9, -8, 10, 11, 9.5, 10.5, 8}; int dif(char*s1,char*s2){ int i,j; for(j=0;j<32;j++) {if(strcmp(s1,time[j])==0) break; } for(i=0;i<32;i++) {if(strcmp(s2,time[i])==0) break; } return (int)((tioff[j]-tioff[i])*60);} int main(){ int n; int hour; int min; char when[15]; char a[5]; char first[10]; char sed[10]; int i,j; int total; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { scanf("%s",when); switch(when[0]){ case 'n':{hour=12;min=0;break;} case 'm':{hour=0;min=0;break;} default:sscanf(when,"%d:%d",&hour,&min); hour%=12;//in case 12:xx p.m. scanf("%s",a); if(a[0]=='p')hour+=12;} scanf("%s %s",first,sed); total=hour*60+min+dif(first,sed); if(total<0) total+=24*60; if(total>=24*60)total-=24*60; hour=total/60; min=total%60; switch(hour){ case 0:{if(min==0){printf("midnight\n");} else {printf("12:%2d a.m.\n",min);} break;} case 12:{if(min==0){printf("noon\n");} else {printf("12:%2d p.m.\n",min);} break;} default:if(hour>12){printf("%d:%2d p.m.\n",hour-12,min);} else {printf("%d:%2d a.m.\n",hour,min);} } } return 0;}提交后WC,找了好久,终于找到原因:
原因1:在求两个时区时差时的函数,最后一步(int)((tioff[i]-tioff[j])*60),实际上应该反过来,即要求的时区减去已知的时区;
原因2:还是输出问题,题目要求输出xx:0x的格式,printf("%d:%02d a.m.\n",hour,min)只会产生xx: x的格式,没有0:,改正:
#include<stdio.h> #include<stdlib.h> #include<string.h> char time[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"}; float tioff[32]={ 0, 0, 1, 1, 0, 1, 1, 2, 2, 3, 3, 4, -4, -3, -3.5, -2.5, -5, -4, -6, -5, -7, -6, -8, -7, -10, -9, -8, 10, 11, 9.5, 10.5, 8}; int dif(char*s1,char*s2){ int i,j; for(j=0;j<32;j++) {if(strcmp(s1,time[j])==0) break; } for(i=0;i<32;i++) {if(strcmp(s2,time[i])==0) break; } return (int)((tioff[i]-tioff[j])*60);} int main(){ int n; int hour; int min; char when[15]; char a[5]; char first[10]; char sed[10]; int i,j; int total; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { scanf("%s",when); switch(when[0]){ case 'n':{hour=12;min=0;break;} case 'm':{hour=0;min=0;break;} default:sscanf(when,"%d:%d",&hour,&min); hour%=12;//in case 12:xx p.m. scanf("%s",a); if(a[0]=='p')hour+=12;} scanf("%s %s",first,sed); total=hour*60+min+dif(first,sed); if(total<0) total+=24*60; if(total>=24*60)total-=24*60; hour=total/60; min=total%60; switch(hour){ case 0:{if(min==0){printf("midnight\n");} else {printf("12:%02d a.m.\n",min);} break;} case 12:{if(min==0){printf("noon\n");} else {printf("12:%02d p.m.\n",min);} break;} default:if(hour>12){printf("%d:%02d p.m.\n",hour-12,min);} else {printf("%d:%02d a.m.\n",hour,min);} } } return 0;}