Posted:

**Thu Jul 05, 2007 11:41 pm**I tryed a lot but I didn 't find where is the mistake in my code. Could someone help me? I'm receiving WA. Thank you in advance.

Code: Select all

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
intcmp (const void *, const void *);
int
main()
{
int time1;
int time2;
int i;
int j;
int quanttimes;
int quantcases;
int nday = 1;
int diff;
int biggdifference;
int napbegin;
int vethours[200];
char auxt1[10];
char auxt2[10];
char temp[4000];
while ((scanf ("%d\n", &quantcases)) != EOF)
{
quanttimes = 0;
//reading the times
for (i = 0; i < quantcases; i++)
{
gets (temp);
//getting the times
sscanf (temp, "%s %s", auxt1, auxt2);
time1 = ((auxt1[0] - 48) * 10 + (auxt1[1] - 48)) * 60 + ((auxt1[3] - 48) * 10 + (auxt1[4] - 48));
vethours[quanttimes++] = time1;
time2 = ((auxt2[0] - 48) * 10 + (auxt2[1] - 48)) * 60 + ((auxt2[3] - 48) * 10 + (auxt2[4] -48));
vethours[quanttimes++] = time2;
}
qsort (vethours,quanttimes,sizeof (int),intcmp);
napbegin = 1080;
//special cases (10:00 and/or 18:00)
biggdifference = vethours[0] - 600;
if ((1080 - vethours[quanttimes - 1]) > biggdifference)
biggdifference = 1080 - vethours[quanttimes - 1];
//finding the big nap
for (i = 1; i < (quanttimes - 1); i += 2)
if ((diff = (vethours[i + 1] - vethours[i])) > biggdifference)
biggdifference = diff;
//finding if there are more than one big nap (picking the earliest)
for (i = 1, j = 0; i < (quanttimes - 1); i += 2)
if ((diff = (vethours[i + 1] - vethours[i])) == biggdifference)
if (vethours[i] < napbegin)
napbegin = vethours[i];
//special cases
if (biggdifference == (vethours[0] - 600))
{
if (vethours[0] < napbegin)
napbegin = vethours[0];
}
else if (biggdifference == (1080 - vethours[quanttimes - 1]))
if (vethours[quanttimes - 1] < napbegin)
napbegin = vethours[quanttimes - 1];
if (biggdifference < 60)
printf ("Day #%d: the longest nap starts at %2d:%02d and will last for %d minutes.\n", nday, (napbegin / 60), (napbegin % 60), biggdifference);
else
{
printf ("Day #%d: the longest nap starts at %2d:%02d and will last for %d hours and %d minutes.\n", nday, (napbegin / 60), (napbegin % 60), (biggdifference / 60), (biggdifference % 60));
}
nday++;
}
return (0);
}
//compare two numbers (integers)
int
intcmp (const void *n1, const void *n2)
{
int *a1;
int *a2;
a1 = (int *) n1;
a2 = (int *) n2;
if (*a1 > *a2)
return 1;
if (*a1 < *a2)
return -1;
return 0;
}
```