## 10371 - Time Zones

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:

### 10371 - Time Zones

Hi!
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;
}
``````
Thanks!

soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:
This problem doesn't need to particular algorithm.

In fact, it's a simple ad hoc problem. But if you want to solve this

problem, you must think carefully about time system.

( Many people take several mistakes about a.m. to p.m. conversion

and rounding hours / minutes. )

Here are sample input and output data. Maybe it gives you a great

chance to solve this problem. Good luck~ : )

Input

213
noon HST CEST
11:29 a.m. EST GMT
11:30 p.m. EST GMT
10:30 p.m. ACDT UTC
10:30 a.m. ACDT UTC
3:30 a.m. NST UTC
3:30 p.m. NST UTC
8:30 p.m. NST UTC
8:30 a.m. NST UTC
midnight NDT EDT
noon NDT EDT
11:45 p.m. EST EDT
11:45 p.m. EST NST
7:30 a.m. CDT EDT
9:00 p.m. MSD CST
12:06 p.m. HST AEST
2:50 p.m. BST CDT
6:30 a.m. PDT EDT
9:33 a.m. PDT HST
11:30 a.m. CET AEST
2:50 p.m. UTC UTC
8:05 p.m. UTC UTC
6:45 p.m. PDT MSK
7:00 p.m. MSK AST
2:19 p.m. MST AST
10:06 p.m. BST AEST
11:00 a.m. CET WEST
1:20 p.m. UTC EEST
12:30 a.m. MDT EDT
9:30 a.m. WET EEST
midnight EST EET
3:59 p.m. EET AEDT
12:30 p.m. WEST ACDT
11:28 p.m. IST EDT
2:00 p.m. CST AEST
midnight EST EET
11:47 p.m. AKST AKDT
noon AEDT AWST
8:30 a.m. CDT MDT
4:00 p.m. WET AKST
8:03 p.m. PDT AKDT
4:30 a.m. MSD MSK
6:00 a.m. CDT AEST
2:48 a.m. NDT GMT
11:10 a.m. EEST ACST
noon CST MSK
6:00 p.m. MDT NST
midnight NST AST
9:00 a.m. CET IST
2:30 p.m. AST MSK
12:07 a.m. AST AEST
4:52 a.m. AKST WET
6:21 a.m. AST AKST
9:44 a.m. BST MST
11:50 p.m. CST EST
8:01 p.m. MST AEDT
1:45 p.m. BST EST
noon ACDT EDT
noon MST AEST
6:55 p.m. NST AEST
5:30 p.m. MDT EEST
noon CST EST
12:30 p.m. MST CET
1:08 a.m. MDT WEST
7:06 p.m. EEST IST
5:10 a.m. AKST CDT
6:45 p.m. AST PDT
4:00 p.m. EDT AEDT
2:08 p.m. CET AKDT
noon BST MST
7:11 a.m. PST CDT
12:29 a.m. MST ADT
9:40 p.m. BST MDT
12:35 a.m. HST ADT
9:09 p.m. HST WEST
3:30 a.m. WEST PST
5:54 p.m. MSK AST
9:30 p.m. AKDT AST
4:32 p.m. AKST CST
3:00 a.m. UTC EEST
6:55 p.m. AKDT AST
9:14 a.m. AKST AST
10:30 p.m. MST HST
1:17 a.m. BST EDT
1:38 a.m. MDT UTC
9:34 a.m. UTC AEDT
10:19 a.m. NDT EDT
1:00 a.m. PST MSK
midnight ACST ACDT
3:01 a.m. AST ACDT
1:30 p.m. MSK CEST
2:40 p.m. ADT CST
9:25 p.m. WET EST
9:30 a.m. PDT AWST
midnight EDT AEDT
7:15 a.m. NST MDT
9:22 p.m. PST ACST
10:13 p.m. ADT NDT
11:17 p.m. AWST EST
noon MSD UTC
1:00 a.m. MSD PST
11:00 p.m. CST CST
midnight EDT AST
12:23 a.m. ACDT CDT
midnight ACST IST
8:00 p.m. NST MSD
7:21 p.m. EET AST
3:32 p.m. CET CEST
1:14 a.m. CST AWST
12:04 p.m. MSD AKDT
7:02 p.m. CDT EDT
4:42 p.m. CEST ADT
7:56 a.m. GMT ADT
11:39 p.m. PST MDT
6:00 a.m. HST CST
9:59 p.m. AKDT HST
12:58 a.m. ACST EEST
midnight PDT CDT
1:00 p.m. BST NST
9:57 p.m. ADT UTC
2:41 a.m. PDT WET
12:30 a.m. EST ACDT
1:00 a.m. AWST MSK
10:00 a.m. AEST AEDT
7:40 p.m. AEST MDT
6:03 p.m. MST IST
12:19 a.m. CST WET
5:28 p.m. ACST PST
11:10 a.m. MDT EET
12:30 a.m. CET MDT
2:48 a.m. NST WET
6:36 a.m. HST BST
9:46 p.m. AKST UTC
8:15 p.m. ACST GMT
8:53 p.m. MDT MSD
8:00 a.m. MSD AEDT
5:34 a.m. CET WEST
2:41 p.m. AST MDT
2:00 a.m. AKST GMT
3:24 p.m. CEST AEST
11:00 a.m. EST AEST
6:30 a.m. PDT ADT
7:32 p.m. MSK PST
noon BST NDT
5:16 p.m. MST AEDT
5:54 p.m. ACDT PDT
12:30 a.m. NST EEST
8:15 p.m. PDT MDT
9:35 a.m. UTC IST
7:00 a.m. ACDT ADT
5:50 a.m. GMT IST
10:44 a.m. NDT UTC
7:00 a.m. CDT ACST
1:15 p.m. MSD CEST
6:00 a.m. MSK CET
11:17 a.m. WET ADT
5:30 p.m. MSK MDT
12:33 a.m. AEDT EDT
10:30 p.m. HST PDT
10:40 p.m. AWST MDT
2:37 a.m. AWST CET
5:00 p.m. ACDT ACST
midnight MSD EST
5:12 a.m. ACDT MDT
noon EDT MSD
6:00 p.m. AEST MDT
3:14 a.m. CDT CST
3:22 p.m. IST MST
10:53 p.m. MDT EDT
2:19 a.m. CDT NDT
11:22 a.m. AKDT EEST
8:00 a.m. WEST MSD
2:25 a.m. CDT EDT
noon MSD BST
1:26 p.m. AKDT AEDT
1:44 a.m. MST EST
11:30 a.m. AEDT ADT
5:05 p.m. HST ADT
11:00 a.m. NDT UTC
4:00 a.m. AST BST
6:00 p.m. AST AST
noon EST AEST
5:00 p.m. CET CDT
12:17 p.m. MSD AKDT
11:20 a.m. AST AKDT
4:30 a.m. WET CET
3:01 a.m. PST EET
4:30 a.m. NST ACST
10:12 p.m. MST CEST
3:30 a.m. WEST AKST
8:30 a.m. EST IST
11:03 a.m. HST EDT
7:00 a.m. GMT CDT
2:00 a.m. CEST CDT
5:39 a.m. ADT EST
11:01 a.m. HST AEST
8:54 p.m. EET CST
3:00 a.m. CST AKST
10:45 p.m. CST WEST
7:32 p.m. ACDT HST
noon AEST CDT
5:34 a.m. AKDT NST
12:30 a.m. EDT BST
5:57 p.m. AKST EET
7:00 p.m. AKDT EEST
2:46 p.m. HST WEST
10:55 a.m. EDT AST
2:40 p.m. MSK MDT
9:00 a.m. NST EST
6:25 p.m. AEST ACDT
10:43 p.m. CST EST

Output

midnight
4:29 p.m.
4:30 a.m.
noon
midnight
7:00 a.m.
7:00 p.m.
midnight
noon
10:30 p.m.
10:30 a.m.
12:45 a.m.
1:15 a.m.
8:30 a.m.
11:00 a.m.
8:06 a.m.
8:50 a.m.
9:30 a.m.
6:33 a.m.
8:30 p.m.
2:50 p.m.
8:05 p.m.
4:45 a.m.
noon
5:19 p.m.
7:06 a.m.
11:00 a.m.
4:20 p.m.
2:30 a.m.
12:30 p.m.
7:00 a.m.
12:59 a.m.
10:00 p.m.
6:28 p.m.
6:00 a.m.
7:00 a.m.
12:47 a.m.
9:00 a.m.
7:30 a.m.
7:00 a.m.
7:03 p.m.
3:30 a.m.
9:00 p.m.
5:18 a.m.
5:40 p.m.
9:00 p.m.
8:30 p.m.
11:30 p.m.
9:00 a.m.
11:16 a.m.
9:30 p.m.
2:07 p.m.
1:52 p.m.
1:21 a.m.
1:44 a.m.
12:50 a.m.
2:01 p.m.
7:45 a.m.
9:30 p.m.
5:00 a.m.
8:25 a.m.
2:30 a.m.
1:00 p.m.
8:30 p.m.
8:08 a.m.
5:06 p.m.
9:10 a.m.
3:45 p.m.
7:00 a.m.
5:08 a.m.
4:00 a.m.
10:11 a.m.
4:29 a.m.
2:40 p.m.
7:35 a.m.
8:09 a.m.
6:30 p.m.
10:54 a.m.
1:30 a.m.
7:32 p.m.
6:00 a.m.
10:55 p.m.
2:14 p.m.
7:30 p.m.
8:17 p.m.
7:38 a.m.
8:34 p.m.
9:00 a.m.
8:49 a.m.
noon
1:00 a.m.
5:31 p.m.
12:30 p.m.
11:40 a.m.
4:25 p.m.
12:30 a.m.
3:00 p.m.
4:45 a.m.
2:52 p.m.
10:43 p.m.
10:17 a.m.
8:00 a.m.
1:00 p.m.
11:00 p.m.
midnight
8:53 a.m.
3:30 p.m.
3:30 a.m.
1:21 p.m.
4:32 p.m.
3:14 p.m.
12:04 a.m.
8:02 p.m.
11:42 a.m.
4:56 a.m.
1:39 a.m.
10:00 a.m.
7:59 p.m.
6:28 p.m.
2:00 a.m.
8:30 a.m.
12:57 a.m.
9:41 a.m.
4:00 p.m.
8:00 p.m.
11:00 a.m.
3:40 a.m.
2:03 a.m.
6:19 a.m.
11:58 p.m.
7:10 p.m.
5:30 p.m.
6:18 a.m.
5:36 p.m.
6:46 a.m.
10:45 a.m.
6:53 a.m.
3:00 p.m.
5:34 a.m.
12:41 p.m.
11:00 a.m.
11:24 p.m.
2:00 a.m.
10:30 a.m.
8:32 a.m.
8:30 a.m.
11:16 a.m.
12:24 a.m.
7:00 a.m.
9:15 p.m.
10:35 a.m.
5:30 p.m.
6:50 a.m.
1:14 p.m.
9:30 p.m.
11:15 a.m.
4:00 a.m.
8:17 a.m.
8:30 a.m.
9:33 a.m.
1:30 a.m.
8:40 a.m.
7:37 p.m.
4:00 p.m.
3:00 p.m.
12:42 p.m.
8:00 p.m.
2:00 a.m.
2:14 a.m.
7:22 a.m.
12:53 a.m.
4:49 a.m.
10:22 p.m.
11:00 a.m.
3:25 a.m.
9:00 a.m.
8:26 a.m.
3:44 a.m.
9:30 p.m.
12:05 a.m.
1:30 p.m.
9:00 a.m.
6:00 p.m.
3:00 a.m.
11:00 a.m.
12:17 a.m.
7:20 a.m.
5:30 a.m.
1:01 p.m.
5:30 p.m.
7:12 a.m.
5:30 p.m.
2:30 p.m.
5:03 p.m.
2:00 a.m.
7:00 p.m.
3:39 a.m.
7:01 a.m.
12:54 p.m.
midnight
5:45 a.m.
11:02 p.m.
9:00 p.m.
10:04 a.m.
5:30 a.m.
4:57 a.m.
6:00 a.m.
1:46 a.m.
10:55 a.m.
5:40 a.m.
7:30 a.m.
6:55 p.m.
11:43 p.m.

TWEmp
New poster
Posts: 7
Joined: Tue Jan 21, 2003 4:52 am
Location: Hong Kong
Contact:

### 10371 problem

Sorry that I have no choice but posting my code here to
check the bugs...

(The problem is Wrong Answer)

Code: Select all

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int Mdiff(char time[]) {
float difference;
int output;
if (strcmp(time, "UTC") == 0) difference = 0.0;
if (strcmp(time, "GMT") == 0) difference = 0.0;
if (strcmp(time, "BST") == 0) difference = 1.0;
if (strcmp(time, "IST") == 0) difference = 1.0;
if (strcmp(time, "WET") == 0) difference = 0.0;
if (strcmp(time, "WEST") == 0) difference = 1.0;
if (strcmp(time, "CET") == 0) difference = 1.0;
if (strcmp(time, "CEST") == 0) difference = 2.0;
if (strcmp(time, "EET") == 0) difference = 2.0;
if (strcmp(time, "EEST") == 0) difference = 3.0;
if (strcmp(time, "MSK") == 0) difference = 3.0;
if (strcmp(time, "MSD") == 0) difference = 4.0;
if (strcmp(time, "AST") == 0) difference = -4.0;
if (strcmp(time, "ADT") == 0) difference = -3.0;
if (strcmp(time, "NST") == 0) difference = -3.5;
if (strcmp(time, "NDT") == 0) difference = -2.5;
if (strcmp(time, "EST") == 0) difference = -5.0;
if (strcmp(time, "EDT") == 0) difference = -4.0;
if (strcmp(time, "CST") == 0) difference = -6.0;
if (strcmp(time, "CDT") == 0) difference = -5.0;
if (strcmp(time, "MST") == 0) difference = -7.0;
if (strcmp(time, "MDT") == 0) difference = -6.0;
if (strcmp(time, "PST") == 0) difference = -8.0;
if (strcmp(time, "PDT") == 0) difference = -7.0;
if (strcmp(time, "HST") == 0) difference = -10.0;
if (strcmp(time, "AKST") == 0) difference = -9.0;
if (strcmp(time, "AKDT") == 0) difference = -8.0;
if (strcmp(time, "AEST") == 0) difference = 10.0;
if (strcmp(time, "AEDT") == 0) difference = 11.0;
if (strcmp(time, "ACST") == 0) difference = 9.5;
if (strcmp(time, "ACDT") == 0) difference = 10.5;
if (strcmp(time, "AWST") == 0) difference = 8.0;
output = (int) (difference * 60.0);
return output;
}

int A;
scanf("%i", &A);
return A;
}

int main(int args, char *argv[]) {
int i = read();
int x, y, z;
char Chk;
char Case[10];
int time;
char zoneA[4];
char zoneB[4];
for (x = 0; x < i; x++) {
scanf("%s", Case);
Chk = Case[0];
if (Chk == 'n') {
time = 12*60;
}
else {
if (Chk == 'm') {
time = 0;
}
else {
y = 0;
while (Case[y] != NULL) y++;
if (y == 5) {
if ((Case[0] == '1') && (Case[1] == '2')) {
time = 0;
}
else time = (Case[1] - 48) * 60 + 600;
z = 3;
}
else {
time = (Case[0] - 48) * 60;
z = 2;
}
time += (Case[z] - 48) * 10 + (Case[z+1] - 48);
scanf("%s", Case);
if (Case[0] == 'p') time += 12*60;
}
}
scanf ("%s %s", zoneA, zoneB);
time += Mdiff(zoneB) - Mdiff(zoneA);
if (time >= 24*60) {
time -= 24*60;
}
else if (time < 0) time += 24*60;
if (time == 0) {
printf("midnight\n");
}
else if (time == 12*60) {
printf("noon\n");
}
}
else if (time < 12*60) {
y = time / 60;
z = time % 60;
if (y == 0) y = 12;
printf("%i:", y);
if (z < 10) printf("0");
printf("%i a.m.\n", z);
}
else {
y = time / 60 - 12;
z = time % 60;
if (y == 0) y = 12;
printf("%i:", y);
if (z < 10) printf("0");
printf("%i p.m.\n", z);
}
}
return 0;
}
``````

Dmytro Chernysh
Experienced poster
Posts: 146
Joined: Sat Apr 26, 2003 2:51 am

### 10371 - REALLY STRANGE !!!

My program(on Pascal) passes all posted inputs at Waterloo site. So, what might be wrong? Or the Judge cann't compare the strings correctly?
Or my mistake is so deep?

LittleJohn
Learning poster
Posts: 83
Joined: Wed Feb 27, 2002 2:00 am
Location: Taiwan
I'm not using Pascal, but I suggest you read http://online-judge.uva.es/p/pascal.html first.
Good Luck!

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm
I use Pascal all the time. That page is out of date since the judge changed from GPC to FPC. String comparison and formatting is quite OK now.

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Contact:

### Thanks!

Thanks a lot soyoja for the sample i/o set.
It is for your sample i/o that I've got AC.

sakhassan
Experienced poster
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU
I am getting lot's of WA ... But cant find ma bug ?? pls can anyone help me ....pls pls

Code: Select all

``````
#include<stdio.h>
#include<string.h>
#include<math.h>

#define N 80

char timezone[][30][10]=
{
{"UTC","0"},
{"GMT","0"},
{"BST","+1"},
{"IST","+1"},
{"WET","0"},
{"WEST","+1"},
{"CET","+1"},
{"CEST","+2"},
{"EET","+2"},
{"EEST","+3"},
{"MSK","+3"},
{"MSD","+4"},
{"AST","-4"},
{"NST","-3.5"},
{"NDT","-2.5"},
{"EST","-5"},
{"EDT","-4"},
{"CST","-6"},
{"CDT","-5"},
{"MST","-7"},
{"MDT","-6"},
{"PST","-8"},
{"PDT","-7"},
{"HST","-10"},
{"AKST","-9"},
{"AKDT","-8"},
{"AEST","+10"},
{"AEDT","+11"},
{"ACST","+9.5"},
{"ACDT","+10.5"},
{"AWST","+8"}

};

int chk(char z[N])
{

int i;

for(i=0;i<=32;i++)
{
if(strcmp(timezone[i][0],z)==0)
return i;
}

return -1;

}

int main()
{

int cases;
char str[N];
char fmt[N],z1[N],z2[N];
int time,h,m;
int idx1,idx2;
double t1,t2;
double zone;
//freopen("in3.cpp","r",stdin);
// freopen("10371.cpp","w",stdout);
scanf("%d",&cases);

while(cases--)
{

scanf("%s",str);
if(strcmp(str,"noon")==0 || strcmp(str,"midnight")==0)
{
scanf("%s%s",z1,z2);
//printf("%s %s %s:",str,z1,z2);
if(strcmp(str,"noon")==0)
time=12*60;
else
time=0;
}
else
{
scanf("%s%s%s",fmt,z1,z2);
sscanf(str,"%d:%d",&h,&m);
//printf("%s %s %s %s:",str,fmt,z1,z2);
if(strcmp(fmt,"a.m.")==0)
{
if(h==12)
h=0;
}
else
h += 12;
time = h*60 + m;
}

idx1 = chk(z1);
idx2 = chk(z2);

sscanf(timezone[idx1][1],"%lf",&t1);
sscanf(timezone[idx2][1],"%lf",&t2);

zone = 0.0;
if(t1<0.0)
zone += fabs(t1);
else
zone -= t1;
if(t2<0.0)
zone -= fabs(t2);
else
zone += t2;

//zone = t1-t2;
//time += zone*60;
time += (zone * 60.0);
if(time<0) time = 1440 + time;
h = (time/60)%24;
m = time%60;

if(h==0 && m==0)
printf("midnight\n");
else if(h==12 && m==0)
printf("noon\n");
else
{

//h = time/(60);
//m = time%(60);
if(h>12)
{
h = h - 12;
strcpy(fmt,"p.m.");
}
else if(h==0)
{
h=12;
strcpy(fmt,"a.m.");
}
else if(h<12)
strcpy(fmt,"a.m.");
else if(h==12)
strcpy(fmt,"p.m.");

printf("%d:%02d %s\n",h,m,fmt);

}

}

return 0;
}

``````

Thanks in advance ....
Time that gone is gone forever ...

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Check the I/O sets...

Input:

Code: Select all

``````5
12:06 p.m. HST AEST
12:30 p.m. WEST ACDT
12:30 p.m. MST CET
12:04 p.m. MSD AKDT
12:17 p.m. MSD AKDT``````
Output:

Code: Select all

``````8:06 a.m.
10:00 p.m.
8:30 p.m.
12:04 a.m.
12:17 a.m.``````
Hope these help.
Ami ekhono shopno dekhi...
HomePage

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

### 10371 - WA

I'm getting WA again and again.
I have tested all the i/o given by SOYOJA and JAN.
( There were some bugs in my code, which I rectified. Thanks to them.)
Now I'm getting same output as theirs...
But still WA
I need some more i/o, where my code fails.

Here is my code:

Code: Select all

``````#include<iostream>		// acm-10371
#include<string>
using namespace std;

#define sz size()

//FILE* fi = freopen ("z.txt", "r", stdin);

void time(string s)
{
bool am=1;
int h,m,i,x=0,y=0;
string in="",out="",t="";

//------------start of input------------

if( s[0] == 'n' )						// noon
{
h=12;
m=0;
am=0;

for(i = 5; s[i]!=' '; i++)
in+=s[i];

i++;

for(; i < s.sz; i++)
out+=s[i];

}
else if( s[0] == 'm' )					// midnight
{
h=12;
m=0;
am=1;

for(i = 9; s[i]!=' '; i++)
in+=s[i];

i++;

for(; i < s.sz; i++)
out+=s[i];

}
else									// others
{
for(i = 0; s[i]!=':' ; i++)
t+=s[i];
sscanf(t.c_str(),"%d",&h);

i++;

t="";
for(; s[i]!=' '; i++)
t+=s[i];
sscanf(t.c_str(),"%d",&m);

i++;

if( s[i] == 'a' )
am=1;
else
am=0;

i+=5;

for(; s[i]!=' '; i++)
in+=s[i];

i++;

for(; i < s.sz; i++)
out+=s[i];

}

//--------------------------------end of input----------------------------------------

// cout<<endl<<h<<" "<<m<<" "<<am<<" "<<in<<" "<<out<<endl;

// ******************************

if( in == "UTC" )
{
x = 0;
y = 0;
}
if( in == "GMT" )
{
x = 0;
y = 0;
}
if( in == "BST" )
{
x = 1;
y = 0;
}
if( in == "IST" )
{
x = 1;
y = 0;
}
if( in == "WET" )
{
x = 0;
y = 0;
}
if( in == "WEST" )
{
x = 1;
y = 0;
}
if( in == "CET" )
{
x = 1;
y = 0;
}
if( in == "CEST" )
{
x = 2;
y = 0;
}
if( in == "EET" )
{
x = 2;
y = 0;
}
if( in == "EEST" )
{
x = 3;
y = 0;
}
if( in == "MSK" )
{
x = 3;
y = 0;
}
if( in == "MSD" )
{
x = 4;
y = 0;
}
if( in == "AST" )
{
x = -4;
y = 0;
}
if( in == "ADT" )
{
x = -3;
y = 0;
}
if( in == "NST" )
{
x = -3;
y = -30;
}
if( in == "NDT" )
{
x = -2;
y = -30;
}
if( in == "EST" )
{
x = -5;
y = 0;
}
if( in == "EDT" )
{
x = -4;
y = 0;
}
if( in == "CST" )
{
x = -6;
y = 0;
}
if( in == "CDT" )
{
x = -5;
y = 0;
}
if( in == "MST" )
{
x = -7;
y = 0;
}
if( in == "MDT" )
{
x = -6;
y = 0;
}
if( in == "PST" )
{
x = -8;
y = 0;
}
if( in == "PDT" )
{
x = -7;
y = 0;
}
if( in == "HST" )
{
x = -10;
y = 0;
}
if( in == "AKST" )
{
x = -9;
y = 0;
}
if( in == "AKDT" )
{
x = -8;
y = 0;
}
if( in == "AEST" )
{
x = 10;
y = 0;
}
if( in == "AEDT" )
{
x = 11;
y = 0;
}
if( in == "ACST" )
{
x = 9;
y = 30;
}
if( in == "ACDT" )
{
x = 10;
y = 30;
}
if( in == "AWST" )
{
x = 8;
y = 0	;
}

// ******************************

//-------------- in --> UTC ------------

if( (m-y) < 0 )		// minutes
{
m = 60 + (m-y);
if( h == 12 )
am = !am;
h--;
}
else if( (m-y) > 60 )
{
m = (m-y) - 60;
if( h == 12 )
am = !am;
h++;
}
else
m = m - y;

if( m==60 )
{
m=0;
if(h==11)
am = !am;
h++;
if(h>12)
h = h - 12;
}

if( (h-x) < 0 )		// hours
{
h = 12 + (h-x);
am = !am;
}
else if( (h-x) == 0 )
{
h = 12 + (h-x);
}
else if( (h-x) > 12 )
{
if( h!=12 )
am = !am;
h = (h-x) - 12;
}
else if( (h-x) == 12 )
{
if( h!=12 )
am = !am;
h = (h-x);
}
else if( (h==12) && ((h-x) < 12) )
{
h = h-x;
am = !am;
}
else
h = h - x;

//------------------------------------------------------------------------------------

// cout<<"in UTC...  ";
// cout<<h<<" "<<m<<" "<<am<<endl;

// ******************************

if( out == "UTC" )
{
x = 0;
y = 0;
}
if( out == "GMT" )
{
x = 0;
y = 0;
}
if( out == "BST" )
{
x = 1;
y = 0;
}
if( out == "IST" )
{
x = 1;
y = 0;
}
if( out == "WET" )
{
x = 0;
y = 0;
}
if( out == "WEST" )
{
x = 1;
y = 0;
}
if( out == "CET" )
{
x = 1;
y = 0;
}
if( out == "CEST" )
{
x = 2;
y = 0;
}
if( out == "EET" )
{
x = 2;
y = 0;
}
if( out == "EEST" )
{
x = 3;
y = 0;
}
if( out == "MSK" )
{
x = 3;
y = 0;
}
if( out == "MSD" )
{
x = 4;
y = 0;
}
if( out == "AST" )
{
x = -4;
y = 0;
}
if( out == "ADT" )
{
x = -3;
y = 0;
}
if( out == "NST" )
{
x = -3;
y = -30;
}
if( out == "NDT" )
{
x = -2;
y = -30;
}
if( out == "EST" )
{
x = -5;
y = 0;
}
if( out == "EDT" )
{
x = -4;
y = 0;
}
if( out == "CST" )
{
x = -6;
y = 0;
}
if( out == "CDT" )
{
x = -5;
y = 0;
}
if( out == "MST" )
{
x = -7;
y = 0;
}
if( out == "MDT" )
{
x = -6;
y = 0;
}
if( out == "PST" )
{
x = -8;
y = 0;
}
if( out == "PDT" )
{
x = -7;
y = 0;
}
if( out == "HST" )
{
x = -10;
y = 0;
}
if( out == "AKST" )
{
x = -9;
y = 0;
}
if( out == "AKDT" )
{
x = -8;
y = 0;
}
if( out == "AEST" )
{
x = 10;
y = 0;
}
if( out == "AEDT" )
{
x = 11;
y = 0;
}
if( out == "ACST" )
{
x = 9;
y = 30;
}
if( out == "ACDT" )
{
x = 10;
y = 30;
}
if( out == "AWST" )
{
x = 8;
y = 0	;
}

// ******************************

//--------------- UTC --> out -----------

if( (m+y) < 0 )		// minutes
{
m = 60 + (m+y);
if( h == 12 )
am = !am;
h--;
}
else if( (m+y) > 60 )
{
m = (m+y) - 60;
if( h == 12 )
am = !am;
h++;
}
else
m = m + y;

if( m==60 )
{
m=0;
if(h==11)
am = !am;
h++;
if(h>12)
h = h - 12;
}

if( (h+x) < 0 )		// hours
{
h = 12 + (h+x);
am = !am;
}
else if( (h+x) == 0 )
{
h = 12 + (h+x);
}
else if( (h+x) > 12 )
{
if( h!=12 )
am = !am;
h = (h+x) - 12;
}
else if( (h+x) == 12 )
{
if( h!=12 )
am = !am;
h = (h+x);
}
else if( (h==12) && ((h+x) < 12) )
{
h = h+x;
am = !am;
}
else
h = h + x;

//------------------------------------------------------------------------------------

// cout<<"final...  ";
// cout<<h<<" "<<m<<" "<<am<<endl<<endl;

//------------------------------------output------------------------------------------

if( h==12 && m==0 )
{
if( am==1 )
printf("midnight\n");
else
printf("noon\n");
}
else
{
printf("%d:",h);

if(m<10)
printf("0");
printf("%d ",m);

if( am==1 )
printf("a.m.\n");
else
printf("p.m.\n");
}

}

int main()
{
string s;
int t;

scanf("%d\n",&t);

while( t-- )
{
getline(cin,s);
time(s);
}
return 0;
}

``````
P.S: Forgive me if my code is confusing.
I am not totally useless, because I can still be used as a bad example

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:

### Re: 10371 - Time Zones

Try the cases.

Input:

Code: Select all

``````2
11:38 p.m. NDT IST
6:50 p.m. CDT ACDT``````
Output:

Code: Select all

``````3:08 a.m.
10:20 a.m.``````
Hope these help.
Ami ekhono shopno dekhi...
HomePage

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

### Re: 10371 - Time Zones

I wonder how you generated those test cases on which my code fails exactly

I've corrected the mistake. And I checked all the previous i/o given by you and soyoja.
But still WA.

Here is my corrected code...

Code: Select all

``````#include<iostream>		// acm-10371
#include<string>
using namespace std;

#define sz size()

// FILE* fi = freopen ("z.txt", "r", stdin);

void time(string s)
{
bool am=1;
int h,m,i,x=0,y=0;
string in="",out="",t="";

//--------------------------------start of input--------------------------------------

if( s[0] == 'n' )						// noon
{
h=12;
m=0;
am=0;

for(i = 5; s[i]!=' '; i++)
in+=s[i];

i++;

for(; i < s.sz; i++)
out+=s[i];

}
else if( s[0] == 'm' )					// midnight
{
h=12;
m=0;
am=1;

for(i = 9; s[i]!=' '; i++)
in+=s[i];

i++;

for(; i < s.sz; i++)
out+=s[i];

}
else									// others
{
for(i = 0; s[i]!=':' ; i++)
t+=s[i];
sscanf(t.c_str(),"%d",&h);

i++;

t="";
for(; s[i]!=' '; i++)
t+=s[i];
sscanf(t.c_str(),"%d",&m);

i++;

if( s[i] == 'a' )
am=1;
else
am=0;

i+=5;

for(; s[i]!=' '; i++)
in+=s[i];

i++;

for(; i < s.sz; i++)
out+=s[i];

}

//--------------------------------end of input----------------------------------------

// cout<<endl<<h<<" "<<m<<" "<<am<<" "<<in<<" "<<out<<endl;

// ******************************

if( in == "UTC" )
{
x = 0;
y = 0;
}
if( in == "GMT" )
{
x = 0;
y = 0;
}
if( in == "BST" )
{
x = 1;
y = 0;
}
if( in == "IST" )
{
x = 1;
y = 0;
}
if( in == "WET" )
{
x = 0;
y = 0;
}
if( in == "WEST" )
{
x = 1;
y = 0;
}
if( in == "CET" )
{
x = 1;
y = 0;
}
if( in == "CEST" )
{
x = 2;
y = 0;
}
if( in == "EET" )
{
x = 2;
y = 0;
}
if( in == "EEST" )
{
x = 3;
y = 0;
}
if( in == "MSK" )
{
x = 3;
y = 0;
}
if( in == "MSD" )
{
x = 4;
y = 0;
}
if( in == "AST" )
{
x = -4;
y = 0;
}
if( in == "ADT" )
{
x = -3;
y = 0;
}
if( in == "NST" )
{
x = -3;
y = -30;
}
if( in == "NDT" )
{
x = -2;
y = -30;
}
if( in == "EST" )
{
x = -5;
y = 0;
}
if( in == "EDT" )
{
x = -4;
y = 0;
}
if( in == "CST" )
{
x = -6;
y = 0;
}
if( in == "CDT" )
{
x = -5;
y = 0;
}
if( in == "MST" )
{
x = -7;
y = 0;
}
if( in == "MDT" )
{
x = -6;
y = 0;
}
if( in == "PST" )
{
x = -8;
y = 0;
}
if( in == "PDT" )
{
x = -7;
y = 0;
}
if( in == "HST" )
{
x = -10;
y = 0;
}
if( in == "AKST" )
{
x = -9;
y = 0;
}
if( in == "AKDT" )
{
x = -8;
y = 0;
}
if( in == "AEST" )
{
x = 10;
y = 0;
}
if( in == "AEDT" )
{
x = 11;
y = 0;
}
if( in == "ACST" )
{
x = 9;
y = 30;
}
if( in == "ACDT" )
{
x = 10;
y = 30;
}
if( in == "AWST" )
{
x = 8;
y = 0	;
}

// ******************************

//------------------------------------ in --> UTC ------------------------------------

if( (m-y) < 0 )		// minutes
{
m = 60 + (m-y);
if( h == 12 )
am = !am;
h--;
if(h==0)
h=12;
}
else if( (m-y) > 60 )
{
m = (m-y) - 60;
if( h == 11 )
am = !am;
h++;
if(h>12)
h = h - 12;
}
else
m = m - y;

if( m==60 )
{
m=0;
if(h==11)
am = !am;
h++;
if(h>12)
h = h - 12;
}

if( (h-x) < 0 )		// hours
{
h = 12 + (h-x);
am = !am;
}
else if( (h-x) == 0 )
{
h = 12 + (h-x);
}
else if( (h-x) > 12 )
{
if( h!=12 )
am = !am;
h = (h-x) - 12;
}
else if( (h-x) == 12 )
{
if( h!=12 )
am = !am;
h = (h-x);
}
else if( (h==12) && ((h-x) < 12) )
{
h = h-x;
am = !am;
}
else
h = h - x;

//------------------------------------------------------------------------------------

// cout<<"in UTC...  ";
// cout<<h<<" "<<m<<" "<<am<<endl;

// ******************************

if( out == "UTC" )
{
x = 0;
y = 0;
}
if( out == "GMT" )
{
x = 0;
y = 0;
}
if( out == "BST" )
{
x = 1;
y = 0;
}
if( out == "IST" )
{
x = 1;
y = 0;
}
if( out == "WET" )
{
x = 0;
y = 0;
}
if( out == "WEST" )
{
x = 1;
y = 0;
}
if( out == "CET" )
{
x = 1;
y = 0;
}
if( out == "CEST" )
{
x = 2;
y = 0;
}
if( out == "EET" )
{
x = 2;
y = 0;
}
if( out == "EEST" )
{
x = 3;
y = 0;
}
if( out == "MSK" )
{
x = 3;
y = 0;
}
if( out == "MSD" )
{
x = 4;
y = 0;
}
if( out == "AST" )
{
x = -4;
y = 0;
}
if( out == "ADT" )
{
x = -3;
y = 0;
}
if( out == "NST" )
{
x = -3;
y = -30;
}
if( out == "NDT" )
{
x = -2;
y = -30;
}
if( out == "EST" )
{
x = -5;
y = 0;
}
if( out == "EDT" )
{
x = -4;
y = 0;
}
if( out == "CST" )
{
x = -6;
y = 0;
}
if( out == "CDT" )
{
x = -5;
y = 0;
}
if( out == "MST" )
{
x = -7;
y = 0;
}
if( out == "MDT" )
{
x = -6;
y = 0;
}
if( out == "PST" )
{
x = -8;
y = 0;
}
if( out == "PDT" )
{
x = -7;
y = 0;
}
if( out == "HST" )
{
x = -10;
y = 0;
}
if( out == "AKST" )
{
x = -9;
y = 0;
}
if( out == "AKDT" )
{
x = -8;
y = 0;
}
if( out == "AEST" )
{
x = 10;
y = 0;
}
if( out == "AEDT" )
{
x = 11;
y = 0;
}
if( out == "ACST" )
{
x = 9;
y = 30;
}
if( out == "ACDT" )
{
x = 10;
y = 30;
}
if( out == "AWST" )
{
x = 8;
y = 0	;
}

// ******************************

//------------------------------------ UTC --> out -----------------------------------

if( (m+y) < 0 )		// minutes
{
m = 60 + (m+y);
if( h == 12 )
am = !am;
h--;
if(h==0)
h=12;
}
else if( (m+y) > 60 )
{
m = (m+y) - 60;
if( h == 11 )
am = !am;
h++;
if(h>12)
h = h - 12;
}
else
m = m + y;

if( m==60 )
{
m=0;
if(h==11)
am = !am;
h++;
if(h>12)
h = h - 12;
}

if( (h+x) < 0 )		// hours
{
h = 12 + (h+x);
am = !am;
}
else if( (h+x) == 0 )
{
h = 12 + (h+x);
}
else if( (h+x) > 12 )
{
if( h!=12 )
am = !am;
h = (h+x) - 12;
}
else if( (h+x) == 12 )
{
if( h!=12 )
am = !am;
h = (h+x);
}
else if( (h==12) && ((h+x) < 12) )
{
h = h+x;
am = !am;
}
else
h = h + x;

//------------------------------------------------------------------------------------

// cout<<"final...  ";
// cout<<h<<" "<<m<<" "<<am<<endl<<endl;

//------------------------------------output------------------------------------------

if( h==12 && m==0 )
{
if( am==1 )
printf("midnight\n");
else
printf("noon\n");
}
else
{
printf("%d:",h);

if(m<10)
printf("0");
printf("%d ",m);

if( am==1 )
printf("a.m.\n");
else
printf("p.m.\n");
}

}

int main()
{
string s;
int t;

scanf("%d\n",&t);

while( t-- )
{
getline(cin,s);
time(s);
}
return 0;
}

``````
I am not totally useless, because I can still be used as a bad example

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm