在某一时刻,时针和分针的夹角会是多少度?具体要求如下,求解!

img


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int x, y, z;
    double ans;
    cin >> x >> y >> z;
    if (x > 12) //x是24小时制
    {
        x = x - 12;
    }
    ans = abs(30 * x - 5.5 * y - (1.0 * 11 / 120) * z); //取绝对值,因为分针角度可能大于时针角度
    if (ans > 180)
    {
        ans = 360 - ans; //夹角取0~180
    }
    cout << (int)ans << endl;
    return 0;
}

img

以0(12)时作为起点,顺时针算作正数,则每过1小时时针走360/12=30度,每1分钟时针走30/60=0.5度,每1秒钟时针走0.5/60=1/120度。
同样的,每1分钟分针走360/60=6度,每1秒钟分针走6/60=0.1度。
拼接起来后(注意对360取模)相减取绝对值即可。

C不熟,大概写了一下。。。

int h, m, s;
float h_angle, m_angle;
int result;
scanf("%d", &h);
scanf("%d", &m);
scanf("%d", &s);
if (h>=12) {
h=h-12;
}
h_angle = h30+m0.5+s1.0/120;
m_angle = m
6+s*0.1;
result = int(fabs(h_angle-m_angle));
if (resul>180) {
result = 360-result;
}
printf("%d", result);