1. 程式人生 > >HDU 5387 Clock(分數類+模擬)

HDU 5387 Clock(分數類+模擬)

textbox ica struct role 2.4 pac eal esp 2.3

題意:

給你一個格式為hh:mm:ss的時間,問:該時間時針與分針、時針與秒針、分針與秒針之間夾角的度數是多少。
若夾角度數不是整數,則輸出最簡分數形式A/B,即A與B互質。

解析:

先計算出總的秒數 S=hh?3600+mm?60+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(" \n"); } return 0; }

HDU 5387 Clock(分數類+模擬)