HDU 5387 Clock(分数类+模拟)

题意:

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

解析:

先计算出总的秒数 S=hh3600+mm60+ss

  1. 由于秒钟每秒走1°,
    所以当前时间,秒钟与12点的度数为 S%360

  2. 由于分针每秒走 0.1°,
    既然已经计算出总秒数,那么当前时间,分针与12点的度数为 S/10%360

  3. 由于时针每秒走(1/120)°。那么当前时间。时针与12点的度数为 S/120%360

然后计算出几个角度之间的绝对值。
又由于题目要求的是劣角,所以推断一下当前求出的角度的绝对值是否大于180°。
假设大于180°,就把当前角度减去180°。

注意:

每行末尾另一个空格,没有输出会PE。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 type;

struct Frac {

    type a, b;

    Frac() {a = 0; b = 1;}
    Frac(type a) {this->a = a; b = 1; }
    Frac(type a, type b) {this->a = a; this->b = b; deal();}

    void init() {a = 0; b = 1;}

    type gcd(type a, type b) {
        while (b) {
            type tmp = a % b;
            a = b;
            b = tmp;
        }
        return a;
    }

    void deal() {
        type d = gcd(a, b);
        a /= d; b /= d;
        if (b < 0) {
            a = -a;
            b = -b;
        }
    }

    Frac operator + (Frac c) {
        Frac ans;
        ans.a = a * c.b + b * c.a;
        ans.b = b * c.b;
        ans.deal();
        return ans;
    }

    Frac operator - (Frac c) {
        Frac ans;
        ans.a = a * c.b - b * c.a;
        ans.b = b * c.b;
        ans.deal();
        return ans;
    }

    Frac operator * (Frac c) {
        Frac ans;
        ans.a = a * c.a;
        ans.b = b * c.b;
        ans.deal();
        return ans;
    }

    Frac operator / (Frac c) {
        Frac ans;
        ans.a = a * c.b;
        ans.b = b * c.a;
        ans.deal();
        return ans;
    }

    Frac operator % (Frac c) {
        Frac ans;
        ans.b = b * c.b;
        ans.a = a * c.b % (c.a * b);
        ans.deal();
        return ans;
    }

    void absolute() {
        if (a < 0) a = -a;
        if (b < 0) b = -b;
    }

    void operator += (Frac c) {*this = *this + c;}
    void operator -= (Frac c) {*this = *this - c;}
    void operator *= (Frac c) {*this = *this * c;}
    void operator /= (Frac c) {*this = *this / c;}

    bool operator > (Frac c) {return a * c.b > b * c.a;}
    bool operator == (Frac c) { return a * c.b == b * c.a;}
    bool operator < (Frac c) {return !(*this < c && *this == c);}
    bool operator >= (Frac c) {return !(*this < c);}
    bool operator <= (Frac c) {return !(*this > c);}
    bool operator != (Frac c) {return !(*this == c);}
    bool operator != (type c) {return *this != Frac(c, 1);}

    void operator = (type c) {this->a = c; this->b = 1;}

    void put() {
        if (a == 0) printf("0");
        else {
            if (b == 1) printf("%I64d", a);
            else printf("%I64d/%I64d", a, b);
        }
    }
};

int t;
type hh, mm, ss;

int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%I64d:%I64d:%I64d", &hh, &mm, &ss);
        type S = hh * 3600 + mm * 60 + ss;
        Frac s = Frac((S * 6) % 360);
        Frac m = Frac(S, 10);
        Frac h = Frac(S, 120);

        m = m % Frac(360);
        h = h % Frac(360);

        Frac a1 = (h - m);
        Frac a2 = (h - s);
        Frac a3 = (m - s);

        a1.absolute(); a2.absolute(); a3.absolute();
        if (a1 > Frac(180)) a1 = Frac(360) - a1;
        if (a2 > Frac(180)) a2 = Frac(360) - a2;
        if (a3 > Frac(180)) a3 = Frac(360) - a3;

        a1.put(); printf(" ");
        a2.put(); printf(" ");
        a3.put(); printf(" 
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/llguanli/p/7258196.html