c++ UVA 579 - ClockHands wrong answer

c++ UVA 579 - ClockHands wrong answer

本文关键字:wrong answer ClockHands UVA c++      更新时间:2023-10-16

我试图解决这个问题,但系统一直给我"错误的答案"。我检查了其他人的解决方案,我确信我的算法是正确的。有人能帮我吗?非常感谢。问题: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("%.3fn", total); 
    }
    return 0;
}

我在中发现了一个错误

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

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

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