579 - Clock Hands

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

Moderator: Board moderators

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

Re: 579 - ClockHands

Post by saiful_islam »

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

Post by Dark Lord »

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
saiful_islam
New poster
Posts: 6
Joined: Tue Feb 08, 2011 7:41 pm

Re: 579 - ClockHands

Post by saiful_islam »

tnx Bro :lol: ACC
20112685
New poster
Posts: 1
Joined: Thu Jan 19, 2012 1:07 pm

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

Post by 20112685 »

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;
}
AT-2012
New poster
Posts: 1
Joined: Sun May 27, 2012 9:58 pm

Re: 579 - ClockHands

Post by AT-2012 »

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

Post by brianfry713 »

Use double instead of float.
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

Post by shellexecutor »

Float is OK. I use float and got AC.
moxlotus
New poster
Posts: 31
Joined: Sat Sep 17, 2011 6:47 am

579 TLE

Post by moxlotus »

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.
:D

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]);
		}
	}
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 579 TLE

Post by brianfry713 »

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

Post by moxlotus »

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

Post by brianfry713 »

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

Post by cyberdragon »

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

Post by brianfry713 »

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

Post by This Is ERFAN »

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

Post by brianfry713 »

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

Return to “Volume 5 (500-599)”