【hdu1006】解方程

http://acm.hdu.edu.cn/showproblem.php?pid=1006

这题坑了我好久,发现居然是一个除法变成了整除,TAT,所以建议在写较长的运算表达式的时候出现了除法尽量加个强制转换,避免写出了错误的代码检查很久还检查不出来。枚举当前所在的小时数h和分钟数m,设分钟数为s时满足三个针之间的夹角大于等于D,则可得到三跟针的所在位置(偏转的角度),然后列出方程min(d1 - d2, 360 - (d1 - d2)) >= d, min(d1 - d3, 360 - (d1 - d3)) >= d, min(d3 - d2, 360 - (d3 - d2)) >= d。可得出s的解集, s0, s1, ..., s5。则最后的答案是(s0 V s1) ^ (s2 V s3) ^ (s4 V s5)的区间长度, 这里有个技巧,注意s0,s1(其它类似)是不相交的,所以可以合并为一类,一共3类,从每一类中选1个共8种情况,可以证明这8种情况的解集互不相交,故可以单独统计。详见代码:

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <map>

 7 #include <stack>

 8 #include <string>

 9 #include <ctime>

10 #include <queue>

11 #define mem0(a) memset(a, 0, sizeof(a))

12 #define mem(a, b) memset(a, b, sizeof(a))

13 #define lson l, m, rt << 1

14 #define rson m + 1, r, rt << 1 | 1

15 #define eps 0.0000001

16 #define lowbit(x) ((x) & -(x))

17 #define memc(a, b) memcpy(a, b, sizeof(b))

18 #define xx(a) ((a) * (a))

19 using namespace std;

20 #define LL __int64

21 #define DB double

22 #define pi 3.14159265359

23 #define MD 10000007

24 #define INF (int)1e9

25 struct S {

26         double l, r;

27         S (double l = 0, double r = 0): l(l), r(r) {}

28 };

29 S jiaoS(S s1, S s2)

30 {

31         return S(max(s1.l, s2.l), min(s1.r, s2.r));

32 }

33 double calc(S a[])

34 {

35         double res = 0;

36         for(int i = 0; i < 8; i++) {

37                 int x1 = i & 1, x2 = ((i & 2) > 0) + 2, x3 = ((i & 4) > 0) + 4;

38                 S ans = jiaoS(a[x1], jiaoS(a[x2], a[x3]));

39                 res += max(0.0, min(60.0, ans.r) - max(0.0, ans.l));

40         }

41         return res;

42 }

43 int main()

44 {

45         //freopen("input.txt", "r", stdin);

46         //freopen("output.txt", "w", stdout);

47         int dd;

48         while(cin>> dd, dd != -1) {

49                 double d = (double)dd;

50                 double ans = 0;

51                 for(int i = 0; i < 12; i++) {

52                         for(int j = 0; j < 60; j++) {

53                                 int h = i, m = j;

54                                 S a[] = {

55                                         S(10.0 * (6 * m + d - 360) / 59, 10.0 * (6 * m - d) / 59),

56                                         S(10.0 * (6 * m + d) / 59, 10.0 * (6 * m + 360 - d) / 59),

57                                         S(120.0 * (30 * h + m / 2.0 + d - 360) / 719, 120.0 * (30 * h + m / 2.0 - d) / 719),

58                                         S(120.0 * (30 * h + m / 2.0 + d) / 719, 120.0 * (30 * h + m / 2.0 + 360 - d) / 719),

59                                         S(120.0 * (30 * h - 11.0 * m / 2 + d - 360) / 11, 120.0 * (30 * h - 11.0 * m / 2 - d) / 11),

60                                         S(120.0 * (30 * h - 11.0 * m / 2 + d) / 11, 120.0 * (30 * h - 11.0 * m / 2 + 360 - d) / 11)

61                                 };

62                                 ans += calc(a);

63                         }

64                 }

65                 printf("%.3f\n", ans * 100 / (12 * 60 * 60));

66         }

67         return 0;

68 }
View Code

 

你可能感兴趣的:(HDU)