HUT 排序训练赛 G - Clock
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Given a sequence of five distinct times written in the format hh : mm , where hh are two digits representing full hours (00 <= hh <= 23) and mm are two digits representing minutes (00 <= mm <= 59) , you are to write a program that finds the median, that is, the third element of the sorted sequence of times in a nondecreasing order of their associated angles. Ties are broken in such a way that an earlier time precedes a later time.
For example, suppose you are given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), you are to report 21:00.
Input
Output
Sample Input
Sample Output
Source
【题目来源】
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41455#problem/G
【题目大意】
输入5个时间点,要求计算时针与分针的夹角,然后比较夹角大小,输出第三大的那个时间点。
【细节】
注意这道题不需要考虑去重,也就是说直接比较输出就可。
还有就是角度相同的时候要比较时针,时针相同时比较分针。
角度最好定义为double型。
当时间大于或等于12时,与12相减然后取绝对值。
【解题思路】首先还是定义一个结构体用来存储输入的数据,然后就是输入,接着计算角度,结构体排序,输出。
【角度计算方法】
大家已经认识了钟表。钟表上的分针、时针在不停息地转动着,两针有时相互重合,有时相互垂直,有时又成一条直线,而求时针、分针形成的各种不同位置所需的时间,就构成了饶有兴趣的时钟问题。
[基础知识]
(1)周角是360°,钟面上有12个大格,每个大格是360°÷12=30°;有60个小格,每个小格是360°÷60=6°。
(2)时针每小时走一个大格(30°),所以时针每分钟走30°÷60=0.5°;分针每小时走60个小格,所以分针每分钟走6°.
一般来说,已知钟面的时刻求时针和分针所成夹角的度数(小于或等于180°的角度),可以找出时针(整时刻)和分针(当前时刻)之间相差的大格或小格数。求出相应度数以后,再减去时针所走的度数(用分针数乘以0.5°)
下面是我的AC代码:
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #define eps 1e-6 using namespace std; struct clock { char st[6]; int h,m; int time; double du; }c[5]; bool cmp(clock a,clock b) { if(a.du<b.du) return true; if(a.du>b.du) return false; if(a.du==b.du) { return a.time>b.time? false:true; } } double cnt(int h,int m) { if(h>=12) h=h-12; double dh=h*30+m*0.5; double dm=m*6; double ma,mi; ma=max(dh,dm); mi=min(dh,dm); double ans=ma-mi; if(ans>=0&&ans<=180); else ans=360-ma+mi; return ans; } int main() { int t; cin>>t; while(t--) { for(int i=0;i<5;i++) { cin>>c[i].st; c[i].h=(c[i].st[0]-'0')*10+(c[i].st[1]-'0'); c[i].m=(c[i].st[3]-'0')*10+(c[i].st[4]-'0'); c[i].time=c[i].h*60+c[i].m; c[i].du=cnt(c[i].h,c[i].m); } sort(c,c+5,cmp); cout<<c[2].st<<endl; } return 0; }
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3588695.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?