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

题目:
1206. Stacking Cylinders
Total: 2614 Accepted: 1102 Rating:
2.4/5.0(14 votes)

Time Limit: 1sec Memory Limit:32MB
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 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 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
Copy sample input to clipboard
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 <vector>
#include <algorithm>
#include <complex>
#include <cstdio>
using namespace std;

typedef complex<double> point;


/*计算以a b两点为圆心圆的上一层的圆的圆心*/
point calcNextOneCircle(const point& a, const point& b)
{
    point midPoint = (a + b) / point(2,0);//得到 a b 两点的中点
    double heightLen = sqrt(4 - norm(a - midPoint));//由勾股定理计算中点距离上一层圆心的距离
    point height = (b - a)*point(0, 1);//得到与向量ab垂直向上的向量
    height = height / abs(height)*heightLen;//利用上面得到的向量的方向和距离大小构建中点到圆心点的距离向量
    return midPoint + height;
}

/*计算下一层的所有圆心*/
vector<point> calcNextAllCircle(const vector<point>& a)
{
    int size = a.size();
    vector<point> next;
    for (int i = 0; i < size-1; i++)
    {
        next.push_back(calcNextOneCircle(a[i], a[i + 1]));
    }
    return next;
}
bool cmp(point a, point b)
{
    return a.real() < b.real();
}
int main()
{
    int testCase;
    cin >> testCase;
    for (int Case = 1; Case <= testCase; Case++)
    {
        int n;
        cin >> n;
        double t;
        vector<point> P;
        for (int i = 0; i < n; i++)
        {
            cin >> t;
            P.push_back(point(t, 1.0));
        }
        sort(P.begin(), P.end(),cmp);//样例有点坑,需要先排好序再处理
        int levelNum = n - 1;
        while (levelNum--)
        {
            vector<point> P1 = calcNextAllCircle(P);
            //P.assign(P1.begin(), P1.end());
            P = P1;
        }
        printf("%d: %.4f %.4f\n", Case, P[0].real(), P[0].imag());
    }

    //system("pause");
}
/*
test sorting:
5
4 1.0 4.4 7.8 11.2
4 1.0 7.8 11.2 4.4
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值