HDU 1209 Clock(模拟)

题目地址:点击打开链接

题意:先按时针和分针的角度排序,如何角度相等则按时间排序

思路:刚开始是想着时针和分针对0点的角度加起来,考虑的情况多且比较复杂,后来角度直接减取绝对值就可

m点n分k秒时
时针从0点转过的角度a = [m + n/60 + k/3600]*30 
= 30m + n/2 + k/120(度)

分针转过的角度b = [n/60 + k/3600]*360 
= 6n + k/10(度) 

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

struct node
{
    int h,s;
    double r;
}a[6];

bool cmp(struct node a,struct node b)
{
    if(a.r != b.r)
        return a.r < b.r;
    else
        return a.h < b.h;
}

int main()
{
    int t;
    int i;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0; i<5; i++)
        {
            scanf("%d:%d",&a[i].h,&a[i].s);
            if(a[i].h >= 12)
            {
                a[i].r = abs(30.0 * (a[i].h-12) + a[i].s / 2.0 - a[i].s * 6.0);
            }
            else
            {
                a[i].r = abs(30.0 * a[i].h + a[i].s / 2.0 -  a[i].s * 6.0);
            }
            if(a[i].r > 180.0)
                a[i].r = 360.0 - a[i].r;
        }
        sort(a,a+5,cmp);
        printf("%02d:%02d\n",a[2].h,a[2].s);
    }
    return 0;
}

错误代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

struct node
{
    int h,s;
    double r;
}a[6];

bool cmp(struct node a,struct node b)
{
    if(a.r != b.r)
        return a.r < b.r;
    else
        return a.h < b.h;
}

int main()
{
    int t;
    int i;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0; i<5; i++)
        {
            scanf("%d:%d",&a[i].h,&a[i].s);
            if(a[i].h >= 12)
            {
                a[i].r = 30.0 * (a[i].h-12) + (60 - a[i].s) * 6.0 + 2.0 * a[i].s;
            }
            else
            {
                a[i].r = 30.0 * a[i].h + (60 - a[i].s) * 6.0 + 2.0 * a[i].s;
            }
            if(a[i].r > 180.0)
                a[i].r = 360.0 - a[i].r;
        }
        sort(a,a+5,cmp);
        printf("%02d:%02d\n",a[2].h,a[2].s);
    }
    return 0;
}


你可能感兴趣的:(HDU 1209 Clock(模拟))