HDU 5387 Clock(模拟)——(多校练习8)

本文介绍了一道关于计算时钟上时针、分针及秒针之间角度的问题,并提供了详细的解题思路和代码实现,包括如何将时间转换为角度、计算夹角并简化为最简分数形式。

Clock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
Give a time.(hh:mm:ss),you should answer the angle between any two of the minute.hour.second hand
Notice that the answer must be not more 180 and not less than 0
 

Input
There are T(1T104) test cases
for each case,one line include the time

0hh<24,0mm<60,0ss<60
 

Output
for each case,output there real number like A/B.(A and B are coprime).if it's an integer then just print it.describe the angle between hour and minute,hour and second hand,minute and second hand.
 

Sample Input
4 00:00:00 06:00:00 12:54:55 04:40:00
 

Sample Output
0 0 0 180 180 0 1391/24 1379/24 1/2 100 140 120
Hint
每行输出数据末尾均应带有空格
 
/*********************************************************************/

题意:给你一个格式为hh:mm:ss的时间,问改时间时针与分针、时针与秒针、分针与秒针之间夹角的度数是多少,若夹角度数不是整数,则输出最简分数形式A/B,即A与B互质。

解题思路:该题的思路很简单,只需先将时针、分针、秒针代表的时间转换成距离0刻度的角度,然后再求两者之间夹角的度数就会方便许多,需要注意,每秒钟,秒针对分针的贡献是0.1°,秒针对时针的贡献是(1/120)°;每分钟,分针对时针的贡献是0.5°。为了使得计算角度时均为整数,本人特地将角度放大了120倍,最后反正是要化成最简分数形式的,故再除以120即可。

其他细节之处会在代码中作出解释。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 1005;
const int inf = 1000000000;
const int mod = 1000000007;
void gcd(int a,int b)//利用辗转相除法求最大公约数,使得分数最简
{
    int m=a,n=b;
    while(a!=b)
        if(a>b)
            a=a-b;
        else
            b=b-a;
    printf("%d/%d ",m/a,n/a);
}
int main()
{
    int t,h,m,s,a,b,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d:%d:%d",&h,&m,&s);
        h%=12;//时钟一圈12小时,故24小时制的时间要转换成12小时制
        h=h*3600+m*60+s;//时针、分针、秒针的角度均放大120倍,使计算过程中不会出现小数
        m=m*720+s*12;
        s*=720;
        //printf("%d %d %d*\n",h,m,s);
        a=abs(h-m);//计算时针与分针之间的夹角
        b=abs(h-s);//计算时针与秒针之间的夹角
        c=abs(m-s);//计算分针与秒针之间的夹角
        if(a>21600)//当夹角超过180°时,通过360°减去当前夹角使夹角小于180°
            a=43200-a;
        if(b>21600)
            b=43200-b;
        if(c>21600)
            c=43200-c;
        //printf("%d %d %d#\n",a,b,c);
        if(a%120)
            gcd(a,120);
        else
            printf("%d ",a/120);
        if(b%120)
            gcd(b,120);
        else
            printf("%d ",b/120);
        if(c%120)
            gcd(c,120);
        else
            printf("%d ",c/120);
        printf("\n");
    }
    return 0;
}

菜鸟成长记


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值