## 579 - Clock Hands

Moderator: Board moderators

saiful_islam
New poster
Posts: 6
Joined: Tue Feb 08, 2011 7:41 pm

### Re: 579 - ClockHands

ok
Last edited by saiful_islam on Fri Mar 30, 2012 10:07 pm, edited 2 times in total.

Dark Lord
New poster
Posts: 2
Joined: Mon Sep 27, 2010 11:27 am

### Re: 579 - ClockHands

Your code is giving these output.
input:
1:59
1:01
-0.000
-36893488.....something.
But the output should be:
65.500
24.500
Try to use fabs function from math.h
remember angle is always <=180.000

saiful_islam
New poster
Posts: 6
Joined: Tue Feb 08, 2011 7:41 pm

### Re: 579 - ClockHands

tnx Bro ACC

20112685
New poster
Posts: 1
Joined: Thu Jan 19, 2012 1:07 pm

### Re: 579 - ClockHands(WHY WA??SOS)

#include<stdio.h>
#include<math.h>
int main(void)
{
int h,m;
double a;
while(scanf("%i:%i",&h,&m)!=EOF && (h||m))
{
a=fabs(5.5*m-30*h);
if(a-180>1e-9)
a=360-a;
printf("%.3f\n",a);
}
return 0;
}

AT-2012
New poster
Posts: 1
Joined: Sun May 27, 2012 9:58 pm

### Re: 579 - ClockHands

Plz!! Someone help me out wid this code. I got WA.
579: Code

#include<stdio.h>
#include<math.h>
int main()
{
float h,m,ang,n;
while(scanf("%f:%f",&h,&m)==2)
{
if(h==0 && m==0) break;
ang=((h*60+m)/2-(m*6));
if(ang>180) ang=360-ang;
if(ang<0) ang=fabs(ang);
printf("%.3f\n",ang);
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 579 - ClockHands

Check input and AC output for thousands of problems on uDebug!

shellexecutor
New poster
Posts: 2
Joined: Sat Sep 08, 2012 3:36 pm

### Re: 579 - ClockHands

Float is OK. I use float and got AC.

moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

### 579 TLE

The java code is getting TLE, can someone tell me how can this piece of code be optimized ?

The equivalent C code has passed the judge without any problem.

Code: Select all

``````import java.util.*;
import java.text.DecimalFormat;
import java.io.IOException;

public class ClockHands
{
public static void main(String[] args) throws IOException
{
String[] arr = line.split("[:\\s]+");
int hr = Integer.parseInt(arr[0]);
int min= Integer.parseInt(arr[1]);
double hrDegree, minDegree, diff;
DecimalFormat df = new DecimalFormat("0.000");
while(hr > 0 || min > 0)
{
if(hr == 12) hr = 0;
hrDegree = ((hr+(min/60.0))/12.0) * 360;
minDegree = (min/60.0) * 360;
diff = hrDegree > minDegree ? hrDegree-minDegree : minDegree-hrDegree;
if(diff > 180) diff = 360 - diff;
System.out.printf("%s\n", df.format(diff));

arr = line.split("[:\\s]+");
hr = Integer.parseInt(arr[0]);
min = Integer.parseInt(arr[1]);
}
}
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 579 TLE

Use Class Main. Java is too slow for some problems.
Check input and AC output for thousands of problems on uDebug!

moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

### Re: 579 TLE

Actually got a few friends helped me optimized the code.
Apparently the use of StringBuilder and printing it out all at once is much faster than calling System.out.printf.
The performance gain is more than 100%

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 579 TLE

You could also try BufferedWriter.
Check input and AC output for thousands of problems on uDebug!

cyberdragon
New poster
Posts: 20
Joined: Fri Aug 30, 2013 5:42 am

### 579 - ClockHands

3rd case in the sample input.
How 8:10 outputs 175, It should be 180 ?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 579 - ClockHands

The big hand at 10 is 60 degrees to the right of 12.
The small hand at 8:10 is 115 degrees to the left of 12.
Check input and AC output for thousands of problems on uDebug!

This Is ERFAN
New poster
Posts: 6
Joined: Thu Dec 04, 2014 11:40 pm

### Re: 579 - Clock Hands

Getting WA...plz help me out.

Code: Select all

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

int main()
{
float a,b;
char c;
while(scanf("%f %c %f",&a,&c,&b)==3)
{
if(a==0&&b==0) return 0;
float d,d1,d2,d3;
if(b==0)
{
if(a<=6) d=a*5*6;
else
d=(12-a)*5*6;
}
else
{
d1=b/2;
d2=b/5;
if(a==d2) d3=0;
else if(a>d2) d3=(a-d2)*5*6;
else if(a<d2) d3=(d2-a)*5*6;

if(a>d2) d=d3+d1;
else if(a==d2) d=d1;
else d=d3-d1;
if(d>180) d=360-d;
}

printf("%0.3f\n",d);

}
return 0;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 579 - Clock Hands

Try using double instead of float.
Check input and AC output for thousands of problems on uDebug!