Can anyone see any case where my code fails? I've got WA!
Code: Select all
/* Time Zones */
#include <stdio.h>
#include <string.h>
char abrev[][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 dif[] = {
0, 0, 60, 60, 0, 60, 60, 120, 120, 180, 180, 240, -240,
-180, -210, -150, -300, -240, -360, -300, -420, -360,
-480, -420, -600, -540, -480, 600, 660, 570, 630, 480
};
char linha[100];
int main() {
int casos;
scanf("%d", &casos);
fgets(linha,100,stdin);
for (int caso = 0; caso < casos; ++caso) {
int hora1, hora2;
char zona1[10], zona2[10];
int i, j;
fgets(linha,100,stdin);
if (strncmp(linha,"mid",3)==0) {
i = 0;
hora1 = 24 * 60;
while (linha[i] != ' ') {
++i;
}
} else if (strncmp(linha,"noon",4) == 0) {
i = 0;
hora1 = 12 * 60;
while (linha[i] != ' ') {
++i;
}
} else {
i = 0;
hora1 = 0;
while (linha[i] >= '0' && linha[i]<='9') {
hora1 = hora1 * 10 + linha[i] - '0';
++i;
}
hora1 *= 60;
if (hora1 == 12 * 60) {
hora1 = 0;
}
hora2 = 0;
++i;
while (linha[i] >= '0' && linha[i] <= '9') {
hora2 = hora2 * 10 + linha[i] - '0';
++i;
}
hora1 += hora2;
++i;
while (linha[i] == ' ') {
++i;
}
if (linha[i] == 'p') {
hora1 += 12 * 60;
} else {
hora1 += 24 * 60;
}
while (linha[i] != ' ') {
++i;
}
}
while (linha[i] == ' ') {
++i;
}
j = 0;
while (linha[i] != ' ') {
zona1[j++] = linha[i++];
}
zona1[j] = 0;
j = 0;
while (linha[i] == ' ') {
++i;
}
while (linha[i]!='\n' && linha[i] &&linha[i]!=' ') {
zona2[j++] = linha[i++];
}
zona2[j] = 0;
for (i=0;;++i) {
if (strcmp(zona1,abrev[i]) == 0) {
hora1 -= dif[i];
break;
}
}
for (i=0;;++i) {
if (strcmp(zona2,abrev[i]) ==0) {
hora1 += dif[i];
break;
}
}
if (hora1 >= 24 * 60) {
hora1 -= 24 * 60;
}
if (hora1 == 0) {
printf("midnight\n");
} else if (hora1 == 12 * 60) {
printf("noon\n");
} else {
if (hora1 > 12 * 60) {
hora1 -= 12 * 60;
int tmp = hora1 / 60;
if (!tmp) {
tmp = 12;
}
printf("%d:%02d p.m.\n",tmp, hora1%60);
} else {
int tmp = hora1 / 60;
if (!tmp) {
tmp = 12;
}
printf("%d:%02d a.m.\n",tmp,hora1%60);
}
}
}
return 0;
}