Cylinders (e.g. oil drums) (of radius 1 foot) are stacked in a rectangular bin. Each cylinder on an upper row rests on two cylinders in the row below. The cylinders in the bottom row rest on the floor. Each row has one less cylinder than the row below.
This problem is to write a program to compute the location of the center of the top cylinder from the centers of the cylinders on the bottom row. Computations of intermediate values should use double precision.
Input
Each data set will appear in one line of the input. An input line consists of the number,n, of cylinders on the bottom row followed by n floating point values giving the x coordinates of the centers of the cylinders (the y coordinates are all 1.0 since the cylinders are resting on the floor (y = 0.0)). The value ofn will be between 1 and 10 (inclusive). The end of input is signaled by a value ofn = 0. The distance between adjacent centers will be at least 2.0 (so the cylinders do not overlap) but no more than 3.4 (cylinders at levelk will never touch cylinders at level k - 2).
Output
Sample Input
4 1.0 4.4 7.8 11.2
1 1.0
6 1.0 3.0 5.0 7.0 9.0 11.0
10 1.0 3.0 5.0 7.0 9.0 11.0 13.0 15.0 17.0 20.4
5 1.0 4.4 7.8 14.6 11.2
0
Sample Output
6.1000 4.1607
1.0000 1.0000
6.0000 9.6603
10.7000 15.9100
7.8000 5.2143
略坑 交错了有10多次 求出最上面的那个圆的坐标 勾股定理
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
double eps=1e-9;
double hh(double a)
{
return sqrt(4-a*a);
}
int main()
{
double a[10000];
int n,i;
double temp1;
while(~scanf("%d",&n),n>0)
{
for(i=1;i<=n;i++)
{
scanf("%lf",&a[i]);
}
sort(a+1,a+n+1);
printf("%.4lf ",(a[1]+a[n])/2);
temp1=1;
for(i=1;i<n;i++)
{
temp1+=hh( (a[i+1]-a[i])/2 );
}
printf("%.4lf\n",temp1+eps);
}
return 0;
}