[sicily online]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

题目分析:

因为题目给出的提示,可以求出所有球的横坐标,而最上面球的y坐标可以根据紧挨着它的左下角的球来求出,所以可以通过dp或者递归都可以做出来

/*
*/
#include<iostream>
#include <iomanip>
#include<stdio.h>
#include<cmath>
#include<iomanip>
#include<list>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <stack>
#include<queue>
#include<string.h>
using namespace std;

typedef struct POINT
{
	double x;
	double y;
}Point;

bool cmp(const Point &x1,const Point &x2)
{
	return x1.x<x2.x;
}

int main()
{
	int n;
	while(cin>>n&&n!=0)
	{
		vector<Point> data(n*n);//描述二维数组,横坐标表示第几行,纵坐标表示第几列
		for(int i=0;i<n;i++)
		{
			cin>>data[i].x;
			data[i].y=1.0;
		}
		sort(data.begin(),data.begin()+n,cmp);
		for(int i=1;i<n;i++) //行遍历
		{
			for(int j=0;j<n-i;j++)//列遍历
			{
				data[i*n+j].x=(data[j].x+data[j+i].x)/2;
				data[i*n+j].y=sqrt(4-(data[i*n+j].x-data[(i-1)*n+j].x)*(data[i*n+j].x-data[(i-1)*n+j].x))+data[(i-1)*n+j].y;
			}//for
		}//end for
		cout<<setiosflags(ios::fixed)<<setprecision(4)<<data[(n-1)*n].x<<" "<<data[(n-1)*n].y<<endl;
	}//end while
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值