本文主要是介绍杭电1006,神题啊!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <iostream>
using namespace std;struct Set { //表示集合[a, b]double a;double b;Set() {}Set(double _a, double _b): a(_a), b(_b) {}
};Set intersection(Set s1, Set s2) {Set s;s.a = s1.a > s2.a ? s1.a : s2.a;s.b = s1.b > s2.b ? s2.b : s1.b;if (s.a > s.b)s.a = s.b = 0.0;return s;
}
Set intersection3(Set s1, Set s2, Set s3) {return intersection(s1, intersection(s2, s3));
}
Set soloveSet(double a, double b, double d) {Set s;if (a > 0) {s = Set((d-b)/a, (360-d-b)/a);} else {s = Set((360-d-b)/a, (d-b)/a);}return intersection(s, Set(0.0, 60.0));
}int main() {double d = 90.0, total;while (cin >> d && d != -1) {total = 0.0;for (int h = 0; h < 12; h++) {for (int m = 0; m < 60; m++) {//下面就要求满足条件的秒的集合,在这里,秒为0-60的实数,即要求一个秒集合//在任意时刻h:m:s,//秒针与00:00:00的角度为s*6,//分针与00:00:00的角度为m*6+(s/60)*6 = m*6 + s*0.1,//时针与00:00:00的角度为30*h+(m/60)*30+(s/3600)*30 = 30*h + m*0.5 + s/120.0//三个针之间的角度分别为,//秒针与分针|6*s - (m*6+s*0.1)| = |5.9s-6m|//秒针与时针|6*s - (30*h + m*0.5 + s/120.0)| = |(719/120)s - (30h+0.5m)|//分针与时针|(m*6 + s*0.1) - (30*h + m*0.5 + s/120.0)| = |(11/120)*s + (5.5m-30h)|//下面就要解一个方程组使得s满足// D <= |5.9s-6m| <= 360 - D ....................(1)// D <= |(719/120)*s - (30h+0.5m)| <= 360 - D.......(2)// D <= |(11/120)*s + (5.5m-30h)| <= 360 -D.......(3)//显然,上面的方程都是型如d1<=|ax+b|<=d2,那么,我们可以用数学基本知识求解x区间//别忘了,区间还必须满足[0, 60)哦double a0 = 5.9, a1 = 719 / 120.0, a2 = 11 / 120.0;double b0 = -6 * m, b1 = -(30.0 * h + 0.5 * m), b2 = 5.5 * m - 30.0 * h;Set s;Set ans[3][2] = {{soloveSet(-a0, -b0, d), soloveSet(a0, b0, d)},{soloveSet(-a1, -b1, d), soloveSet(a1, b1, d)},{soloveSet(-a2, -b2, d), soloveSet(a2, b2, d)}};for (int i = 0; i < 2; i++)for (int j = 0; j < 2; j++)for (int k = 0; k < 2; k++) {s = intersection3(ans[0][i], ans[1][j], ans[2][k]);total += s.b - s.a;}}}total = total * 100.0 / (12 * 3600);printf("%.3lf\n",total);}return 0;
}
这篇关于杭电1006,神题啊!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!