579 - Clock Hands
Moderator: Board moderators
-
- 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.
Re: 579 - ClockHands
Your code is giving these output.
input:
1:59
1:01
your output:
-0.000
-36893488.....something.
But the output should be:
65.500
24.500
Try to use fabs function from math.h
and visit this page http://en.wikipedia.org/wiki/Clock_angle_problem
remember angle is always <=180.000
input:
1:59
1:01
your output:
-0.000
-36893488.....something.
But the output should be:
65.500
24.500
Try to use fabs function from math.h
and visit this page http://en.wikipedia.org/wiki/Clock_angle_problem
remember angle is always <=180.000
-
- New poster
- Posts: 6
- Joined: Tue Feb 08, 2011 7:41 pm
Re: 579 - ClockHands
tnx Bro
ACC
![:lol:](./images/smilies/icon_lol.gif)
Re: 579 - ClockHands(WHY WA??SOS)
I don't know why I get WA though I have check many inputs and outputs.Please help me,thanks in advance!
#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;
}
#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;
}
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;
}
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 579 - ClockHands
Use double instead of float.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 2
- Joined: Sat Sep 08, 2012 3:36 pm
Re: 579 - ClockHands
Float is OK. I use float and got AC.
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.
Thanks in advance.
The equivalent C code has passed the judge without any problem.
Thanks in advance.
![:D](./images/smilies/icon_biggrin.gif)
Code: Select all
import java.util.*;
import java.text.DecimalFormat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ClockHands
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
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));
line = br.readLine();
arr = line.split("[:\\s]+");
hr = Integer.parseInt(arr[0]);
min = Integer.parseInt(arr[1]);
}
}
}
-
- 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!
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%
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%
-
- 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!
-
- 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 ?
How 8:10 outputs 175, It should be 180 ?
-
- 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.
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!
-
- New poster
- Posts: 6
- Joined: Thu Dec 04, 2014 11:40 pm
Re: 579 - Clock Hands
Getting WA...plz help me out.
![:(](./images/smilies/icon_frown.gif)
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;
}
-
- 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!