SOJ 1012

来源:互联网 发布:淘宝没发货退款 信誉 编辑:IT博客网 时间:2019/10/14 18:38

1012. Stacking Cylinders

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

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 of n will be between 1 and 10 (inclusive). The end of input is signaled by a value of n = 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 level k will never touch cylinders at level k – 2).

Output

The output for each data set is a line containing the x coordinate of the topmost cylinder rounded to 4 decimal places, a space and the y coordinate of the topmost cylinder to 4 decimal places. Note: To help you check your work, the x-coordinate of the center of the top cylinder should be the average of the x-coordinates of the leftmost and rightmost bottom cylinders.

Sample Input

4 1.0 4.4 7.8 11.21 1.06 1.0 3.0 5.0 7.0 9.0 11.010 1.0 3.0 5.0 7.0 9.0 11.0 13.0 15.0 17.0 20.45 1.0 4.4 7.8 14.6 11.20

Sample Output

6.1000 4.16071.0000 1.00006.0000 9.660310.7000 15.91007.8000 5.2143
  这题比较简单,由已知的底层球的坐标依次推出上层球的坐标,直到最顶层,只是要注意题目给出底层球的坐标时未必是按照从左往右的顺序,所以要对底层球的坐标进行排序再计算上层坐标。
// Problem#: 1012// Submission#: 4933205// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<iostream>#include<cmath>#include<iomanip>#include<algorithm>using namespace std;class circle{    public:        double x;        double y;        ~circle(){}        circle(){        }        circle(double X,double Y):x(X),y(Y){        }};circle cor[11][11];//cor[i][j]保存第i层的第j个球 double getx(circle a,circle b)//计算球a,球b之间的上一层的球的球心的x坐标 {    double l=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));    return (a.x+b.x)/2-(b.y-a.y)/2/l*sqrt(16-pow(l,2));}double gety(circle a,circle b)//计算球a,球b之间的上一层的球的球心的y坐标  {    double l=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));    return (a.y+b.y)/2+(b.x-a.x)/2/l*sqrt(16-pow(l,2));}int main(){    int n;    double x;    while(cin>>n&&n)    {        double *p=new double[n];//动态数组存储最底层球的x坐标         for(int i=0;i<n;++i)        {            cin>>x;            p[i]=x;        }        sort(p,p+n);//对p数组进行排序,由于x坐标的输入顺序未必是从左到右         for(int i=1;i<=n;++i)        {            cor[1][i]=circle(p[i-1],1.0);        }        for(int i=2;i<=n;++i)        {            for(int j=1;j<=n-i+1;++j)            {                cor[i][j]=circle(getx(cor[i-1][j],cor[i-1][j+1]),gety(cor[i-1][j],cor[i-1][j+1]));//用循环依次求出上一层每个球的球心坐标,直到最顶层             }        }        cout<<fixed<<setprecision(4)<<cor[n][1].x<<" "<<cor[n][1].y<<endl;            }}                                 

Submit


 

0 0