2013
12-21

# Iron Chain

In physics class, the teacher shows us an interesting physics experiment. He hangs an iron chain on a fixed point on the wall. (Look at the right picture for more details). If the lengths of the iron chains on the left side and right side are different, the gravity forces the iron chain to drop to the floor and hits the sensor. Then the computer shows the F-t functional image on the screen. The teacher wants to teach you momentum and its conservation:
But you are not interested in this at all. The thing you want to know is how long it takes for the iron chain to drop to the floor.
You can assume that the iron chain is well-mixed, and you can ignore the friction between the iron chain and the fixed point on the wall. The friction between the chain and the air needn’t be taken into account either. The fixed point is a particle whose radius is 0. And the acceleration of gravity, or so-called g, is 9.80665m/s2 in this problem.

The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.
Each case contains three integer numbers a, b, h. Look at the picture above for their meaning. 1<=a+b<=5000,a+b<=h<=10000.

For each case, if the chain can reach the floor, print” The iron chain will reach the floor after t seconds.”, otherwise, print” The iron chain will not reach the floor at all!” instead. And the answer should be rounded to three digits to the right of the decimal point. Use the format in the sample.

2
0 10 20
1 3 4

Case 1: The iron chain will reach the floor after 1.428 seconds.
Case 2: The iron chain will reach the floor after 0.595 seconds.

#include <stdio.h>
#include <math.h>

const double g = 9.80665;
const double eps = 1e-6;

int main()
{
int test, t = 1;
double a, b, h, tmp;
double d, l, ans, v;
scanf("%d", &test);
while(test--)
{
scanf("%lf%lf%lf", &a, &b, &h);
if(fabs(a - b) < eps)
{
printf("Case %d: The iron chain will not reach the floor at all!\n", t++);
continue;
}
if(a > b)
{
d = a;
a = b;
b = d;
}
d = 0.0005;
ans = 0.0;
//v = sqrt(2 * g * l * (b - a + l) / (a + b));
tmp = 2 * g / (a + b);
double v1 = 0, v2 = 0;
for(l = d;l <= a;l += d)
{
v2 = sqrt(tmp * l * (b - a + l));
v = (v1 + v2) / 2;
ans += d / v;
v1 = v2;
}
//v = sqrt(tmp * a * b);
double aa = g / 2, bb = v, cc = -(h - b - a);
ans += (-bb + sqrt(bb * bb - 4 * aa * cc)) / 2 / aa;
printf("Case %d: The iron chain will reach the floor after %.3lf seconds.\n", t++, ans);
}
return 0;
}