Description
There is an analog clock with two hands: an hour hand and a minute hand. The two hands form an angle. The angle is measured as the smallest angle between the two hands. The angle between the two hands has a measure that is greater than or equal to 0 and less than or equal to 180 degrees.
Given a sequence of five distinct times written in the format hh : mm , where hh are two digits representing full hours (00 <= hh <= 23) and mm are two digits representing minutes (00 <= mm <= 59) , you are to write a program that finds the median, that is, the third element of the sorted sequence of times in a nondecreasing order of their associated angles. Ties are broken in such a way that an earlier time precedes a later time.
For example, suppose you are given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), you are to report 21:00.
Input
The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case is given on a single line, which contains a sequence of five distinct times, where times are given in the format hh : mm and are separated by a single space.
Output
Print exactly one line for each test case. The line is to contain the median in the format hh : mm of the times given. The following shows sample input and output for three test cases.
Sample Input
3
00:00 01:00 02:00 03:00 04:00
06:05 07:10 03:00 21:00 12:55
11:05 12:05 13:05 14:05 15:05
Sample Output
02:00
21:00
14:05
#include <iostream>
#include <cmath>
using namespace std;
double jao(double m,double n){
double x,y,z;
if(m>12)
m=m-12;
x=m*30+n*0.5;
y=n*6;
z=fabs(x-y);
if(z>180)
z=360-z;
return z;}
int main(){
int T;
cin>>T;
while(T--){
int i=0,j;
double m=0,n=0,tep,k,t;
char a[5][10];
for(;i<5;i++)
cin>>a[i];
double c[5],b[5],d[5];
for(i=0;i<5;i++){
m=0;
n=0;
for(j=0;j<2;j++){
m=m*10+a[i][j]-'0';}
b[i]=m;
for(j=3;j<5;j++){
n=n*10+a[i][j]-'0';}
c[i]=n;}
for(i=0;i<5;i++){
d[i]=jao(b[i],c[i]);}
for(i=0;i<4;i++){
for(j=i+1;j<5;j++){
if(d[i]>d[j]||d[i]==d[j]&&b[i]>b[j]||b[i]==b[j]&&c[i]>c[j]){
tep=d[i];
d[i]=d[j];
d[j]=tep;
k=b[i];
b[i]=b[j];
b[j]=k;
t=c[i];
c[i]=c[j];
c[j]=t;}}}
if(b[2]<10)
cout<<0<<b[2]<<':';
else
cout<<b[2]<<':';
if(c[2]<10)
cout<<0<<c[2]<<endl;
else
cout<<c[2]<<endl;}}
求大佬查錯
uj5u.com熱心網友回復:
if( d[i]>d[j] || d[i]==d[j] && b[i]>b[j] || b[i]==b[j] && c[i]>c[j] )這么長的判斷關系,沒有括號分開, 確定這個對嗎?
uj5u.com熱心網友回復:
#include <iostream>#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
double angle(int hour, int minute){
double x,y,z;
if(hour>12) hour=hour-12;
x = hour*30 + minute*0.5;
y = minute*6;
z = fabs(x-y);
if(z>180) z=360-z;
return z;
}
struct Time {
double angle;
int hours;
int minutes;
} times[5];
#define EPSLON 1e-8
bool compare ( Time a, Time b) {
if ( fabs(a.angle-b.angle) <= EPSLON ) {
if (a.hours == b.hours ) return a.minutes < b.minutes;
return a.hours < b.hours;
} else
return a.angle < b.angle;
}
int main(){
int T;
cin>>T;
while(T--){
int i=0;
for(;i<5;i++) {
scanf("%d:%d", ×[i].hours, ×[i].minutes);
times[i].angle = angle(times[i].hours, times[i].minutes);
}
sort(times, times+5, compare);
printf("%02d:%02d\n", times[2].hours, times[2].minutes);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/154363.html
標籤:C++ 語言
上一篇:c語言
下一篇:最近用python做圓的提取,用了霍夫檢測圓發現效果不是很好(因為要提取的圓很多)一張圖片大概5,6K個吧,初學給點建議吧,謝謝哥哥們
