天天看點

杭電水題--1029 Clock

題目位址:http://acm.hdu.edu.cn/showproblem.php?pid=1209

大意是,給你5個時間,都是mm::nn格式的,按照時針與分針之間的夾角排序,如果相等則取時間小的,輸出中間的那個時間。

挺簡單的一道水題,剛好熟悉一下才學的strtok函數。

唯一需要注意的是在輸出的時候由于strtok函數已經修改了原始的字元串,是以需要重新格式化輸出一下:

代碼如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>


using namespace std;
char str[6];
struct clock
{
    int h,m;
    int time;//總時間
    double du;//角度
}data[5];

bool cmp(clock a,clock b)
{
     if(a.du==b.du)
         return a.time<b.time;
     else
	 return a.du<b.du;
}

double angle(int h,int m)
{
    if(h>=12) h=h-12;
    double deg_h=h*30+m*0.5;//時針角度
    double deg_m=m*6;//分針角度
    double ans=abs(deg_h-deg_m);//內插補點即為夾角
    if(ans>=0&&ans<=180);
    else
        ans=360-ans;
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<5;i++)
        {
            scanf("%s",&str);
			char *p;
			p=strtok(str,":");
			sscanf(p,"%d",&data[i].h);
			p=strtok(NULL,":");
			sscanf(p,"%d",&data[i].m);
            data[i].time=data[i].h*60+data[i].m;
            data[i].du=angle(data[i].h,data[i].m);
        }
        sort(data,data+5,cmp);
	printf("%02d:%02d\n",data[2].h,data[2].m);//輸出需要另外考慮下。
    }

    return 0;
}