c ++ UVA 579 - ClockHands错误答案

时间:2013-09-04 05:19:46

标签: c++

我试图解决这个问题,但系统一直给我“错误答案”。我检查了其他人的解决方案,我确信我的算法是正确的。任何人都可以帮助我吗?非常感谢。 问题: UVA 579 ClockHands

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int hour, minute;
    float hour_degree, minute_degree;
    float total;
    while(scanf("%d:%d",&hour, &minute) == 2)
    {
        if( hour == 0 && minute == 0)
            break;
        minute_degree = minute * 6;
        hour_degree = hour * 30 + float(minute / 2);
        total = fabs(hour_degree - minute_degree);
        if(total > 180)
            total = fabs(360 - total);
        printf("%.3f\n", total); 
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

我发现了一个错误

hour_degree = hour * 30 + float(minute / 2);

你正在进行整数除法,如果minute是奇数,则会出错。它应该是

hour_degree = hour * 30 + float(minute / 2.0);
相关问题