sicily 1012. Stacking Cylinders & 1206. Stacking Cylinders
|
Time Limit: 1sec Memory Limit:32MB
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
Copy sample input to clipboard
4 1.0 4.4 7.8 11.2 Sample Output
6.1000 4.1607 |
分析:
1. 首先,根据题意,程序的输入就是最底层的球,然后在其上面每层都比下一层少一个球,
. 可以证明,在底层的球每一个和其底层第一个球的横坐标的中点不为都有一个其上面球的横坐标,最后一个球对应最上面一个球,如图:
也就是利用等腰三角形来求解。

其实应该说首先要证明第一行的球其从第二个开始,每一个和第一个求,上一层的最左边球都会构成一个等腰三角形,根据这个性质去做就ok,
不要把题意理解为输入的球就是所有球,我开始就是这么理解的所以和一个小伙伴一起想了很久想了一个很复杂的办法,因为那样的话根本不知道分层情况,当然还是利用等腰三角形去解。然后在去饭堂吃饭的时候突然想明白了,,,
code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio> using namespace std; #define RADIUS 2.0 int main(int argc, char const *argv[])
{
vector<double> xValue;
int pointNum;
double x, y; while (cin >> pointNum && pointNum != ) {
xValue.resize(pointNum);
y = RADIUS / ;
for (int i = ; i != pointNum; ++i) {
cin >> xValue[i];
}
sort(xValue.begin(), xValue.end());
x = xValue[]; for (int i = ; i != pointNum; ++i) {
double preX = x;
x = (xValue[i] - xValue[]) / + xValue[]; // 上一层的最左边球的横坐标,注意这个上一层是递增的
preX = x - preX;
y = sqrt(pow(RADIUS, ) - pow(preX, )) + y; // 上一层的最左边球的纵坐标,注意这个上一层是递增的,利用勾股定理
}
printf("%.4lf %.4lf\n", x, y);
}
return ;
}
至于 . Stacking Cylinders 这道题,其实方法是完全一样的,但是输入和输出格式都不一样,我看了好久才发现输入格式不一样。。。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio> using namespace std; #define RADIUS 2.0 int main(int argc, char const *argv[])
{
vector<double> xValue;
int pointNum;
double x, y; int testNum;
cin >> testNum;
for (int i = ; i <= testNum; ++i) {
cin >> pointNum;
xValue.resize(pointNum);
y = RADIUS / 2.0;
for (int i = ; i != pointNum; ++i) {
cin >> xValue[i];
}
sort(xValue.begin(), xValue.end());
x = xValue[]; for (int i = ; i != pointNum; ++i) {
double preX = x;
x = (xValue[i] - xValue[]) / 2.0 + xValue[];
preX = x - preX;
y += sqrt(pow(RADIUS, 2.0) - pow(preX, 2.0));
}
printf("%d: %.4lf %.4lf\n", i, x, y);
}
return ;
}
sicily 1012. Stacking Cylinders & 1206. Stacking Cylinders的更多相关文章
- 层叠上下文 Stacking Context
层叠上下文 Stacking Context 在CSS2.1规范中,每个盒模型的位置是三维的,分别是平面画布上的x轴,y轴以及表示层叠的z轴.对于每个html元素,都可以通过设置z-index属性来设 ...
- Dream team: Stacking for combining classifiers梦之队:组合分类器
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- Stacking调参总结
1. 回归 训练了两个回归器,GBDT和Xgboost,用这两个回归器做stacking 使用之前已经调好参的训练器 gbdt_nxf = GradientBoostingRegressor(lear ...
- 集成学习中的 stacking 以及python实现
集成学习 Ensemble learning 中文名叫做集成学习,它并不是一个单独的机器学习算法,而是将很多的机器学习算法结合在一起,我们把组成集成学习的算法叫做“个体学习器”.在集成学习器当中,个体 ...
- 【书签】stacking、blending
读懂stacking:模型融合Stacking详解/Stacking与Blending的区别 https://blog.csdn.net/u014114990/article/details/5081 ...
- 【集成学习】:Stacking原理以及Python代码实现
Stacking集成学习在各类机器学习竞赛当中得到了广泛的应用,尤其是在结构化的机器学习竞赛当中表现非常好.今天我们就来介绍下stacking这个在机器学习模型融合当中的大杀器的原理.并在博文的后面附 ...
- visual formatting model (可视化格式模型)【持续修正】
概念: visual formatting model,可视化格式模型 The CSS visual formatting model is an algorithm that processes a ...
- ORACLE 移动数据文件 控制文件 重做日志文件
ORACLE数据库有时候需要对存储进行调整,增加分区.IO调优等等,此时需要移动数据文件.重做日志文件.控制文件等等,下文结合例子总结一下这方面的知识点. 进行数据文件.重做日志文件.控制文件的迁移前 ...
- 关于z-index的总结
z-index的作用 很多时候需要把一个元素覆盖到另一个元素之上,比如登入弹出框等,这个时候就需要z-index属性出场了.所以呢,z-index就是调节层的显示优先级,决定哪个显示在最上方.作用范围 ...
随机推荐
- Atom Editor 插件 atom-less 的使用方法
一.下载安装 atom-less atom-less 项目在这里:https://github.com/schmuli/atom-less 安装方法请参考这篇博文:http://blog.csdn.n ...
- Andorid API Package ---> android
包名: android Added in API level 1 URL:http:// ...
- [牛客Wannafly挑战赛27D]绿魔法师
description newcoder 给你一个空的可重集合\(S\). \(n\)次操作,每次操作给出\(x\),\(k\),\(p\),执行以下操作: \(opt\ 1\):在S中加入x. \( ...
- [洛谷P4081][USACO17DEC]Standing Out from the Herd
题目大意:给你$n$个字符串,对每个字符串求出只在这个字符串中出现的字串的个数 题解:先建广义$SAM$,然后对每个点统计一下它的子树中是不是都是在同一个字符串中的,是的话,就把这个点标成这一个字符串 ...
- harbor1.4.0高可用部署
一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Parent 构造函数成为 Children 的方法,然 ...
- 洛谷 [SCOI2010]股票交易 | 单调性DP
题目链接 #include<cstdio> #include<algorithm> #include<cstring> #define N 2005 using n ...
- linux 文件检索操作
linux命令太多了,作为一个后端开发人员,常用的也就这几个 uname -a 查看版本 tail tail -f /data/wealth-consignment-service/logs/stat ...
- bzoj3232圈地游戏——0/1分数规划+差分建模+判环
Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意一个格点出发,沿着格线行走直到 ...
- Linux之poll与select20160619
使用非阻塞I/O的应用程序通常会使用select()和poll()系统调用查询是否可对设备进行无阻塞的访问,这两个系统调用最终又会引发设备驱动中的poll()函数被执行 如果当前不可读(先调用驱动.p ...
- 谈谈CSS中em与px的差异
在国内网站中,包括三大门户,以及“引领”中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使用em作为字体单位, ...