HROJ 201802283 时钟
Description:
兔小灰来到了第三关。。。
邪恶的WSH大魔王默念着咒语,崩的一声,从天而降一个巨大的时钟。 兔小灰开始观察这个时钟, 和普通的机械手表一模一样, 三个指针:
时针,分针和秒针。那么问题来了。给定一个时间t,然后规定表盘上的两个位置,分别是入口和出口。 现在 兔小灰在入口处,如果他只能沿钟表的刻度行走那么他能不能毫无阻碍(中间没有任何指针)地走到出口呢?
Input:
多组数据,处理到文件结尾结束。
对于每组数据,每行有5个整数分别是:h m s d1 d2
其中hⓂ️s为给定的时间t, d1 d2表示入口和出口
(1≤h≤12, 0≤m,s≤59, 1≤d1,d2≤12, d1≠d2).
Output:
如果可以没有阻碍地从出口走到入口 输出 YES。 否则输出 NO。
Sample Input:
12 30 45 3 11
12 0 1 12 1
3 47 0 4 9
Sample Output:
NO
YES
YES
Hint:
对于三个样例,对应分别表示下图
题目链接
这道题目思路是先把时钟的所有点摊开展为一个数轴,当三个针全部都在起点和终点两点之间或者两侧时可以毫无阻碍的走到出口,否则不能。
在数轴上找出起点、终点、三个针的位置然后用一个bool函数判断是否满足条件即可。
最开始写这道题的时候一直WA是因为在计算时针、分针时没有考虑分、秒的影响。
时针的位置是时+(分对时针的影响),分针的位置时分+(秒对分针的影响)。
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1000;
const double eps = 1e-5;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
double locate_start, locate_end;
bool Judge(double a, double b, double c) {
if (a >= locate_start && a <= locate_end) {
if (b >= locate_start && b <= locate_end) {
if (c >= locate_start && c <= locate_end) {
return 1;
}
}
}
if (a <= locate_start || a >= locate_end) {
if (b <= locate_start || b >= locate_end) {
if (c <= locate_start || c >= locate_end) {
return 1;
}
}
}
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int h, m, s, d1, d2;
while (cin >> h >> m >> s >> d1 >> d2) {
double locate_h, locate_m, locate_s;
locate_start = d1 < d2 ? d1 : d2;
locate_end = d1 > d2 ? d1 : d2;
locate_s = (double)s / 60.0 * 12.0;
//cout << "locate_s=" << locate_s << endl;
locate_m = (double)m / 60.0 * 12.0 + (double)s / 60.0 * (1.0 / 60.0);
//cout << "locate_m=" << locate_m << endl;
locate_h = (double)h + (double)m / 60.0 * 1;
//cout << "locate_h=" << locate_h << endl;
if (Judge(locate_h, locate_m, locate_s)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
return 0;
}