## 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

efr_shovo
New poster
Posts: 38
Joined: Wed Sep 22, 2004 9:09 am
Thanks Man I got Accepted Now

Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria
Hi !

The I/O was useful for me too.

Thank you, Ghust_omega !

Lord Nemrod
New poster
Posts: 12
Joined: Mon Mar 28, 2005 7:55 pm
Contact:

### 579 - WHAT"S WRONG???!!!!

I've been trying to sord out what's wrong with this problem for two weeks, but in vain - WA:( . Can you please help me?

Here's the code:

#include "iostream.h"
#include "stdio.h"
#include "iomanip.h"

double GetAngle(int Hour, int Min);

int main(int argc, char* argv[])
{
int i=0;
int Hour = 0;
int Min = 0;
char* time = new char;
cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(3);
while(true)
{
cin>>time;
sscanf(time,"%i:%i", &Hour, &Min);
if(Hour==0)
{
break;
}
if(Hour==12)
{
Hour = 0;
}
cout<<GetAngle(Hour, Min)<<"\n";
}

delete [] time;

return 0;
}

double GetAngle(int Hour, int Min)
{
double HourAngle = 30*Hour + (double)Min/2.0;
double MinAngle = 6*Min;

double Angle = HourAngle-MinAngle;
if(Angle<0)
Angle*=-1;
if(Angle>180)
Angle = 360 - Angle;
return Angle;
}
[/b]

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:
Dear Lord Nemrod,
I tried to compare your code and mine with some test cases, but could not find any difference between the outputs. This is strange that your code is getting WA. Do you mail your code to the OJ ? then try using the submit o matic.

The only case where I found your output to differ with mine is :

12 <some spaces> : <some spaces> 00

The output should be 0.000 but your program terminates on reaching this point. I am not sure if this is a valid input or not, but it is always better to be cautious.

I did not see any output formatting in your code. May be using

Code: Select all

printf("%.3f",angle);
will do the trick. There is no harm in trying.

elgeish
New poster
Posts: 3
Joined: Mon Mar 01, 2004 12:36 am
i've tried ur input and i got ur ACC output but it gives WA???

Code: Select all

#include <stdio.h>

void main() {
int H, M;
float A;

while( scanf( " %i : %i", &H, &M ) && ( H || M ) ) {
A = H * 30 + M / 2.0f - M * 6;
A = A < 0 ? -A : A;
printf( "%.3f\n", A > 180 ? 360.0 - A : A );
}
}


ranban282
New poster
Posts: 37
Joined: Tue May 02, 2006 1:01 pm
Contact:

### 579 WHY TLE???

Here's the code

Code: Select all

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<cctype>
using namespace std;

int main()
{

string s;
int  hr,m;
char temp;
while(cin>>s)
{

if(s=="0:00")
break;
if(s.length()==4)
{
hr=s-'0';
m=(s-'0')*10+s-'0';
}
else if(s.length()==5)
{
hr=s-'0'+10*(s-'0');

m=(s-'0')*10+s-'0';
}
else
break;
if(hr==0 && m==0)
break;
long double hr1=(long double)hr;
long double m1=(long double) m;
printf("%0.3Lf\n",min(fabs(hr1*30.0+m1/2.0-m1*6.0),360.0-fabs(hr1*30.0+m1/2.0-m1*6.0)));
}
return 0;
}


Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada
Looking at your code, maybe there are some whitespaces around? If last line is " 0:00" it will never exit? I am not sure how reading behind EOF works in C++ (Java would crash).

ranban282
New poster
Posts: 37
Joined: Tue May 02, 2006 1:01 pm
Contact:

### HI,

Could you be a bit more specific? As in what kind of inputs are expected?

ranban282
New poster
Posts: 37
Joined: Tue May 02, 2006 1:01 pm
Contact:

### I modify the program, still i get TLE...

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<cctype>
using namespace std;

int main()
{

string s;
while(getline(cin,s,'\n'))
{

int hr=0,m=0;
string::iterator i;

for(i=s.begin();i!=s.end()&&*i!=':';i++)
{
if(isdigit(*i))
hr=hr*10+*i-'0';
}
for(;i!=s.end();i++)
{
if(isdigit(*i))
m=m*10+*i-'0';
}

if(hr==0 )
break;
long double hr1=(long double)hr;
long double m1=(long double) m;
printf("%0.3Lf\n",min(fabs(hr1*30.0+m1/2.0-m1*6.0),360.0-fabs(hr1*30.0+m1/2.0-m1*6.0)));
}
return 0;
}

ranban282
New poster
Posts: 37
Joined: Tue May 02, 2006 1:01 pm
Contact:

### Hi

Thx all, my code got accepted....
You will not believe it.....
The error was caused by using fabs.
The moment i removed it it started working.
Does anyone know why that happens?

comaniac
New poster
Posts: 2
Joined: Sun Jul 22, 2007 7:24 pm
Location: Taiwan,Taipei
Contact:
Thanks a lot!
I found that my code has a small problem through your data
now i got "Accept",thanks.

P.S. That data was created by another random program?

euleramon
New poster
Posts: 6
Joined: Sun Feb 27, 2005 7:02 am

### 579 - ClockHands

Could anyone give me some tips about my code ? I just tested my program with others' test data without problems. But I got the RE(Runtime Error) from Uva system. Please tell me what's wrong with my program.

Code: Select all


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define NUM 9999
int  main()
{
int h[NUM], m[NUM], i=0;
double h_hand[NUM], m_hand[NUM], angle[NUM];

scanf("%d:%d",&h[i], &m[i]);
while(m[i]!=0 || h[i]!=0)
{
h_hand[i] = h[i]*30.0 + m[i]/2.0;
m_hand[i] = m[i]*6.0;
angle[i] = fabs(h_hand[i]-m_hand[i]);

if(angle[i]>180.0)
angle[i] = 360.0 - angle[i];

//	printf("%.3f\n",angle[i]);

i++;
scanf("%d:%d",&h[i], &m[i]);
}

for(int j=0; j<i; j++)
{
printf("%.3f\n",angle[j]);
}
return 0;
}


Before One Piece

euleramon
New poster
Posts: 6
Joined: Sun Feb 27, 2005 7:02 am

### Re: 579 - ClockHands

I just removed the arrays like "[NUM]", and modified my I/O to one output following one input. Then, I got AC. Although I ever kept arrays and modified my I/O to one output following one input similarly, I still got RE. I thought there is sth wrong with my array. Anyway thank everyone for skimming over my code. Before One Piece

jk4837
New poster
Posts: 3
Joined: Sat Aug 04, 2007 6:44 am

### 579 - ClockHands WA ><

I got WA in 579-ClockHands, but can't find any error.
Please help me!

My code:

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
double h,m;
int i;
string c;
cin >> c;
if(c.substr(0,4)=="0:00")
exit(0);
while(1)
{
h=0;
i=0;
double s;
while(isdigit(c))
h=h*10+int(c[i++]-48);
m=int(c[++i]-48)*10+int(c[++i]-48);
//cout << h << " : " << m << endl;
if(h==0 && m==0)
break;
s= m-(h*5.0+m/12)*6;
s= s<0 ? -s : s;
if(s>180)
s=360-s;
cout << fixed << showpoint << setprecision(3) << s ;
cin >> c;
if(c.substr(0,4)!="0:00")
cout << endl;
else
break;
}
return 0;
}

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

### Re: 579 - ClockHands

Why WA plz help

Code: Select all

#include<stdio.h>
int main()
{
//freopen("G:\\in.txt","r",stdin);
//freopen("G:\\out.txt","w",stdout);

long double B,a,d;
while(1)
{
char c;
int m,h,b,M,u=1,i=1,k;
scanf("%d%c%d",&h,&c,&m);
if(h==0)
return 0;
M= (int) m/5;
b=m%5;
a=(long double) m*6;
if(h>=M)
{
B=(long double) (h-M)*5*6-(b*6)+(30*a/360);
if(B>180)
{
printf("%.3Lf\n",360-B);
}
else
printf("%.3Lf\n",B);
}
else
{
B=(long double) ((M-h)*5*6+(b*6)-(30*a/360));
if(B>180)
{
printf("%.3Lf\n",360-B);
}
else
printf("%.3Lf\n",B);

}
}
return 0;
}