hdu5387 鐘錶指針之間夾角(分數計算,模擬)

題意: 給你一個24格式的數字時間,(字符串),問你這個時刻時針與分針 時針與秒針 分針與秒針 之間的夾角,ios

咱們發現 秒針每秒轉6度,分針每秒轉1/10度,每分轉6度,時針每小時轉30度,每分轉1/2度,沒秒轉spa

1/120 度,那麼咱們將錶盤看作一個座標系,12點鐘爲起點,那麼能夠計算出每一個指針這一刻的角度是多少,可是爲了不指針

分數的加減,咱們將全部的數通分,同時乘以120,那麼圓盤一圈360 度變成了360*120 =43200度,確定在int內,將時間轉化blog

爲12制的時間,計算角度,而後作差獲得角度,可是輸出是範圍是0-180 ,因此要求小角,選擇 min(x,43200-x)做爲差值ci

,而後要化簡爲最簡分式就能夠了,(分子是120)字符串

假設當前時間爲 H:M:X
時針此時的度數:
image
分針此時的度數:
image
秒針此時的度數:
imageget

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
int gcd(int a,int b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
int main()
{
    int h,m,s;
    int H,M,S;
    int ans1,ans2,ans3;
    char str[20];
    int t;
    cin>>t;
    while(t--)
    {
        cin>>str;
        h=(str[0]-'0')*10+(str[1]-'0');
        m=(str[3]-'0')*10+(str[4]-'0');
        s=(str[6]-'0')*10+(str[7]-'0');
         if(h>=12)
            h-=12;
 
        //cout<<h<<endl;
         H=h*3600+m*60+s;
        M=m*720+12*s;
        S=s*720;
 
        ans1=max(H,M)-min(H,M);
        ans2=max(H,S)-min(H,S);
        ans3=max(M,S)-min(M,S);
 
        ans1=min(ans1,43200-ans1);
        ans2=min(ans2,43200-ans2);
        ans3=min(ans3,43200-ans3);
 
        int g1,g2,g3;
        g1=gcd(ans1,120);
        g2=gcd(ans2,120);
        g3=gcd(ans3,120);
 
        ans1/=g1;
        ans2/=g2;
        ans3/=g3;
 
        int f1,f2,f3;
        f1=120/g1;
        f2=120/g2;
        f3=120/g3;
 
        if(f1==1)
        {
            cout<<ans1<<" ";
         }
 
        else
        {
            if(ans1==0)
             {
                cout<<0<<" ";
            }
            else
                cout<<ans1<<"/"<<f1<<" ";
        }
 
         if(f2==1)
        {
            cout<<ans2<<" ";
        }
 
        else
        {
            if(ans2==0)
                 cout<<0<<" ";
            else
                cout<<ans2<<"/"<<f2<<" ";
        }
 
        if(f3==1)
        {
             cout<<ans3<<" "<<endl;
        }
 
        else
        {
             if(ans3==0)
                cout<<0<<" "<<endl;
             else
                cout<<ans3<<"/"<<f3<<" "<<endl;
        }
    }
    return 0;
}string

相關文章
相關標籤/搜索