sicily 1206. Stacking Cylinders

原创 2015年11月18日 17:09:26

1206. Stacking Cylinders

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Problem 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 and do not roll from their original positions. 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

The input begins with a line containing the count of problem instances, nProb , as a decimal integer, (1<=nProb<=1000) . This is followed by nProb input lines. An input line consists of the number, n , of cylinders on the b

ottom 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 distance between adjacent centers will be at least 2.0 (so the cylinders do not overlap) and at most 3.4 (so cylinders at level k cannot touch cylinders at level k - 2 ).

Output

The output for each data set is a line containing the problem number (1...nProb) , a colon, a space, the x coordinate of the topmost cylinder to 4 decimal places, a space and the y coordinate of the topmost cylinder to 4 decimal places (both x and y ROUNDED to the nearest digit).

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

5 
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 11.2 14.6

Sample Output

1: 6.1000 4.1607 
2: 1.0000 1.0000 
3: 6.0000 9.6603 
4: 10.7000 15.9100 
5: 7.8000 5.2143
 


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;


void solve(double x1, double y1, double x2, double y2, double &x3, double &y3) {
	double ab = x2 - x1;
	double bc = abs(y2 - y1);
	double ac = sqrt(ab * ab + bc * bc);
	double ad = ac / 2.0;
	double df = sqrt(2.0 * 2.0 - ad * ad);
	double ef = ab * df / ac;
	double de = sqrt(df * df - ef *ef);

	// 判断左边定点靠下还是右边定点靠下
	double lower = y1 > y2 ? y2 : y1;
	y3 = lower + bc / 2.0 + ef;

	// 根据三角形朝向不同,用不同的方式解x

	// 上图左边情况
	if (lower == y1) x3 = ab / 2.0 + x1 - de;
	// 右边情况
	else x3 = ab / 2.0 + x1 + de;
}

int main() {
	int t;
	cin >> t;
	int n = 0;
	while (t--) {
		n++;
		int num;
		cin >> num;
		double x[10], y[10];
		for (int i = 0; i < num; i++) {
			cin >> x[i];
			y[i] = 1.00;
		}
		sort(x, x + num);
		for (int i = num - 1; i >= 1; i--) {
			for (int j = 0; j < i; j++) {
				solve(x[j], y[j], x[j + 1], y[j + 1], x[j], y[j]);
			}
		}
		cout << n << ": " << setprecision(4) << fixed << x[0] << " " << y[0] << endl;
	}
	return 0;
}


sicily 1206. Stacking Cylinders

解题思路:(学习了别人的方法,原文地址http://hi.baidu.com/watermoon_littlegui/blog/item/ce42cfecd2c20edcb21cb1d6.html):...

Sicily 1206. Stacking Cylinders | 使用STL的complex库实现简单几何

题目: 1206. Stacking Cylinders Total: 2614 Accepted: 1102 Rating: 2.4/5.0(14 votes)Time L...
  • linzch3
  • linzch3
  • 2017年06月28日 21:28
  • 118

sicily 1012/1206 Stacking Cylinders

1206. Stacking Cylinders Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description ...

Sicily 1206 Stacking Cylinders

由下面两个圆圆心的坐标求上面那个圆的坐标,如图,由(x1, y1)(x2, y2)求(x3, y3) 别被输入示例骗了,有乱序的情况。 #include #include #inc...
  • wbchou
  • wbchou
  • 2013年02月20日 21:28
  • 246

Sicily 1206 Stack Cylinders

终于有空做做算法分析与设计的作业了。。。然后发现这道居然是个解析几何。。注意到连续三层共四个圆柱体之间之间的顶点关系,用中点公式可以很轻松算出来缺失那点的坐标,显然那个点就是所需要的。应该挺清楚了。。...
  • zaxguo
  • zaxguo
  • 2014年12月15日 10:20
  • 308

1012-Stacking Cylinders

  • 2013年12月05日 14:51
  • 491B
  • 下载

zoj 2403 || poj 2194 || poj 2850 Stacking Cylinders

买一赠二啊。。。。T T 。。。poj这两题居然完全一样,就输入方式不一样,情何以堪!什么质量啊。。给你最后一排球的坐标,求最顶层的球的坐标。我直接一排一排模拟的,无奈用的三角函数,精度死活达不到,W...

POJ 2850 Stacking Cylinders 简单计算几何

计算几何的题目 :   一层一层的算    算到最上面的一层时  pre  == behind    就退出循环输出 涉及到向量的旋转  和三角函数   下面看代码: #include #includ...

SOJ 1012 Stacking Cylinders

题目传送门在此 似乎是是dao合体

acdream 1234 Two Cylinders

Two Cylinders Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1206. Stacking Cylinders
举报原因:
原因补充:

(最多只允许输入30个字)