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

mystical_liu
New poster
Posts: 9
Joined: Sun Jul 21, 2002 1:18 pm

579 - Clock Hands

Post by mystical_liu »

this is my code !!!
is there anybody can tell me where am i wrong!!!

[cpp]#include<stdio.h>
main()
{
int h,m,hn,mn;
float ans;
while(scanf(" %d:%d",&h,&m)==2)
{
if(!h&&!m)break;
ans=((h*60)*0.5)+(m*0.5)-(m*6);
if(ans>180)ans=360.000-ans;
else if(ans<0)ans*=-1;
printf("%.3f\n",ans);
}
}[/cpp]
Revenger
Experienced poster
Posts: 132
Joined: Sun Apr 14, 2002 12:27 pm
Location: Russia

Post by Revenger »

For example, on the test

Code: Select all

1:59
the corect output is

Code: Select all

65.500
Change your code is so way:

[cpp]if(ans<0)ans*=-1;
if(ans>180)ans=360.000-ans;[/cpp]
[/code]
mystical_liu
New poster
Posts: 9
Joined: Sun Jul 21, 2002 1:18 pm

OH!!!I GOt IT!!

Post by mystical_liu »

OH!!!
I got it !!!!!
thinks a lot!!~!~
I know where am I wrong!!
thinks your remide!!!
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

579Why TL?!!

Post by Morning »

[cpp]
#include<stdio.h>
void main()
{
float clock,minute;
float degree;
while(scanf("%f:%f",&clock,&minute)==2)
{
if(!clock&&!minute) break;
degree=(clock*30+minute/2)-minute*6;
while(degree<0)
{
degree+=180;
}
while(degree>=180)
{
degree=360-degree;
}
printf("%.3f\n",degree);
}
}
[/cpp]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Tanzim-Saqib
New poster
Posts: 10
Joined: Thu Jun 05, 2003 5:23 pm
Location: CS, AIUB, Dhaka, Bangladesh
Contact:

Post by Tanzim-Saqib »

I don't understand why these while loops (except for taking inputs) are necessary. Morning, just derive an equation where there will only be minutes and hour and nothing else, no more loops. Hope it helps.
[Tanzim Saqib]
-----------------------------
Problemsetter of 10490
Website: http://tanzim.tk
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

thanx,i've gotten it:)
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

579 WA ><

Post by cytmike »

[cpp]#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int p,h;
char y;
cin>>p>>y>>h;
while ((p!=0)&&(y!=0))
{
long double l=abs(p*30-h*5.5);
if (l>180)
l=360-l;
cout<<setiosflags(ios::showpoint|ios::fixed)<<setprecision(3)<<l<<endl;
cin>>p>>y>>h;
}
return 0;
}

[/cpp]

What happened!!??
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

[c]1:01[/c]
should output
[c]24.500[/c]
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

my program outputs 24.500 too...

:cry:
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

Ah, I think I see the problem.

When I originally compiled your program I got the following warning:
g++ test.cpp
test.cpp: In function `int main()':
test.cpp:13: warning: `double' used for argument 1 of `abs(int)'
However, when I #include <cmath>, it works as expected.

The judge is probably doing the same thing. There are overloaded abs() operators in the <cmath> header file, but the default std::abs only takes and returns an int, which is probably causing your WA.
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

but why i can compile it correctly, but the judge cannot??? :evil:
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

In your environment, your compiler might auto-include <cmath> which has the overloaded operators, but the judge's system, as well as most standard UNIX systems, will not.
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

ic
thank you very much
efr_shovo
New poster
Posts: 38
Joined: Wed Sep 22, 2004 9:09 am

579

Post by efr_shovo »

/* Why 579 Gives Wrong Ans Please Help*/

#include<stdio.h>
#include<ctype.h>

char time1[4];

int hour,minute,mutex,ex;
float deg;

int Digit(int *d)
{
int digit;
int g=*d;
int b10=0;
while(isdigit(time1[g]))
{
b10*=10;
digit=(int(time1[*d])-48)+b10;
b10=digit;
g++;
*d=g;
}
return digit;
}

void main()
{

while(1)
{ int i=0;
scanf("%s",time1);

for(i=0;i<4;i++)
{
if(!mutex)
{
if(isdigit(time1))
{
hour=Digit(&i);
mutex=1;
}
}
else
{
minute=Digit(&i);
mutex=0;
}
}

if(!hour && !minute)
break;
if(hour>12)
hour=0;
else
hour=12-hour;

deg=float((hour*30)+(minute*6+minute/60)-(.5*float(minute)));
if(deg<0)
deg*=-1;
if(deg>180.0)
{
deg=float(360)-deg;
deg*=-1;

}
printf("%.3f\n",deg);
}
}
Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Post by Ghust_omega »

Hi!! efr_shovo try with this I/O

Input:

Code: Select all

0:46
11:55
1:55
12:12
1:00
2:00
0:19
12:46
9:00
10:16
7:00
3:00
3:50
2:43
10:55
9:42
1:18
5:27
10:16
9:22
1:33
3:59
4:37
5:44
4:30
1:46
0:20
2:33
11:50
6:00
10:45
10:27
1:00
4:00
11:17
12:15
0:45
3:27
9:44
7:50
00:00
ouput:

Code: Select all

107.000
27.500
87.500
66.000
30.000
60.000
104.500
107.000
90.000
148.000
150.000
90.000
175.000
176.500
2.500
39.000
69.000
1.500
148.000
149.000
151.500
125.500
83.500
92.000
45.000
137.000
110.000
121.500
55.000
180.000
52.500
151.500
30.000
120.000
123.500
82.500
112.500
58.500
28.000
65.000
Hope its helps
Keep posting!! :D
Post Reply

Return to “Volume 5 (500-599)”