Online coding practice

I was trying to solve a problem on codechef named "Chef and Girlfriend".My solution got rejected, so I went to submissions to look at a solution. After going through it my best guess is that both my code and that are doing exactly the same things.I tried providing same custom inputs to both the codes and got the same exact outputs.

Can you please help me identify what my code is doing different than the other one.

Here is my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

int setTime(char time[],int i,int j)
{
  	return (10*((int)time[i]-48) + ((int)time[j]-48));
}

int main()
{
	int t;
  	scanf("%d",&t);
  	while(t--)
    {
      	int h1,m1,h2,m2,dis,gap,plan2;
      	char time[6];

      	scanf("%s",time);
      	h1 = setTime(time,0,1);
      	m1 = setTime(time,3,4);
      	scanf("%s",time);
      	h2 = setTime(time,0,1);
      	m2 = setTime(time,3,4);
      	scanf("%d",&dis);
      	
      	gap = (h1-h2)*60 + (m1-m2);
      	
        printf("%.1f ",(float)(gap+dis));
      	plan2 = 2*dis<=gap ? gap : (gap/2 + dis);
      	printf("%.1f\n",(float)plan2);
    }
	return 0;
}


And here is the code that I found

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
main()
{
int t;
scanf("%d",&t);
while(t--)
{
	float t1[2],t2[2];float c2,d,c1;
	scanf("%f:%f",&t1[0],&t1[1]);
	scanf("%f:%f",&t2[0],&t2[1]);
	scanf("%f",&d);
	 c1=(t1[0]-t2[0])*60+t1[1]-t2[1] +d;
	if((t1[0]-t2[0])*60+t1[1]-t2[1]>=2*d)
	c2=(t1[0]-t2[0])*60+t1[1]-t2[1];
	else
	c2=d+((t1[0]-t2[0])*60+t1[1]-t2[1])/(float)2;
	printf("%0.1f %0.1f\n",c1,c2);
}
}
> And here is the code that I found
That code reads floats, not integers.

> scanf("%s",time);
> h1 = setTime(time,0,1);
> m1 = setTime(time,3,4);
That code doesn't assume that times are always dd:dd format.

> char time[6];
Or even that times will always fit in 6 characters.
@salem c
here is the link to the problem

https://www.codechef.com/problems/GERALD04

The constraints of the problem ensure that the time is given in HH:MM format. And I tried after changing data type to float for variables "gap" and "plan2" to float . No luck.
Last edited on
> And I tried after changing data type to float for variables "gap" and "plan2" to float . No luck.
But I bet you're still doing integer division.
Topic archived. No new replies allowed.