Vous êtes sur la page 1sur 1

roblem explanation:

Lets suppose we have a clock that has an hour hand 3 units long, a minute hand 4
units long, and a second hand 5 units long. The hour hand moves once every hour,
the minute hand moves once every minute, and the second hand moves once every
second. Therefore, exactly every second, the triangle defined by the ends of the
hands changes its area.

Which is the maximum area between two given times?

Input: A sequence of hours in sets of 2 in the format hh:mm:ss.

Output: The maximum area defined by the ends of the hands in square units.

For a more comprehensive explanation:


https://jutge.org/problems/P17681_en/statement

My code:

I have written a function that takes as arguments the position of the hands in
radians and returns its area (trigonometrically intensive).

double area(double secRad, double minRad, double houRad){


double alfaSM = atan( (5-4*cos(abs(secRad-minRad)))/(4*sin(abs(secRad-minRad)))
);
double alfaSH = atan( (5-3*cos(abs(secRad-houRad)))/(3*sin(abs(secRad-houRad)))
);
double alfaMH = atan( (4-3*cos(abs(minRad-houRad)))/(3*sin(abs(minRad-houRad)))
);

double distSM = abs(5*sin(alfaSM)+4*sin(abs(secRad-minRad)-alfaSM));


double distSH = abs(5*sin(alfaSH)+3*sin(abs(secRad-houRad)-alfaSH));
double distMH = abs(4*sin(alfaMH)+3*sin(abs(minRad-houRad)-alfaMH));

double s = (distSM+distSH+distMH)/2;

return s*(s-distSM)*(s-distSH)*(s-distMH);
}
My main function reads the input from the user and calculates the area for each
second in the range of time the user has entered, stores the maximum area and
finally prints it.

int main(){
char z;
double hI,mI,sI, hF,mF,sF;
while(cin >> hI >> z >> mI >> z >> sI >> hF >> z >> mF >> z >> sF){
double maxArea = 0;
for(int i = 3600*hI+60*mI+sI; i <= 3600*hF+60*mF+sF; i++){
double cacheArea = area( 2*pi*((double)(i%60)/60) , 2*pi*(double)((i
%3600)/60)/60, 2*pi*(double)((i%43200)/3600)/12);
if(cacheArea > maxArea) maxArea = cacheArea;
}
cout << fixed << setprecision(3) << sqrt(maxArea) << endl;
}
}

Vous aimerez peut-être aussi