Title: c++ UVA 579 - ClockHands wrong answer
Tags: c++
Question: I was trying to solve this question but the system keep giving me "wrong answer". I checked other people's solution and I'm sure my algorithm is correct. Can anyone help me with it? thanks a lot.
Question: UVA 579 ClockHands
#include <iostream&gt;
#include <cstdio&gt;
#include <cmath&gt;
using namespace std;
int main()
int hour, minute;
float hour_degree, minute_degree;
float total;
while(scanf("%d:%d",&amp;hour, &amp;minute) == 2)
if( hour == 0 &amp;&amp; minute == 0)
break;
minute_degree = minute * 6;
hour_degree = hour * 30 + float(minute / 2);
total = fabs(hour_degree - minute_degree);
if(total &gt; 180)
total = fabs(360 - total);
printf("%.3f\n", total);
return 0;
Comment: I compiled your program with g++4.4 and the output is exaclty as expected

Here is the accepted answer: I spotted a bug here
hour_degree = hour * 30 + float(minute / 2);
You are doing integer division, and it goes wrong if minute is odd number. It should be
hour_degree = hour * 30 + float(minute / 2.0);
Comment for this answer: You can use `(minutes / 2.0f)` without explicit cast. The parentheses is not necessary but for the sake of readability.
Comment for this answer: It works!!! Thanks a lot! So do I still need to convert the number to float if I use "minute/2.0"??


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:09/04/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++>', 'Title': 'c++ UVA 579 - ClockHands wrong answer', 'ViewCount': 963, 'id': 18605916, 'user_AboutMe': '', 'user_AccountId': 3259517.0, 'user_DisplayName': 'CocoHot', 'user_Reputation': 1201.0, 'user_Views': 55.0, 'user_id': 2745345.0}}