当前位置 : 主页 > 编程语言 > c语言 >

ZOJ Monthly, August 2014 ZOJ - 3806 计算几何+二分

来源:互联网 收集:自由互联 发布时间:2023-09-06
A triangle is one the basic shapes in geometry. It’s a polygon with three vertices and three sides which are line segments. A triangle with vertices A, B, C is denoted ΔABC. And its three sides, BC, CA, AB are often denoted a, b and c. T

A triangle is one the basic shapes in geometry. It’s a polygon with three vertices and three sides which are line segments. A triangle with vertices A, B, C is denoted ΔABC. And its three sides, BC, CA, AB are often denoted a, b and c.

The incircle of a triangle is the largest circle contained in the triangle, it is tangent to the three sides. The center of the incircle is called the triangle’s incenter and the radius of the incircle is called inradius, which is denoted r.

The circumcircle of a triangle is the circle which passes through all its three vertices. The center of the this circle is called circumcenter and its radius is called circumradius, which is denoted R.

It’s very easy to calculate r and R after knowing a, b and c. Now you are given r and R, can you calculate a valid triple (a, b, c)?

Input
There are multiple cases. Each case has two positive integers r and R in one line. (r and R are less than 105)

Ouput
For each case, print three floating numbers a, b and c in one line if it’s possible to get r and R. If there are no possible tuples, print “NO Solution!”.

The judge program uses your a, b and c to calculate the inradius and circumradius. Only the relative error of your inradius and circumradius less than 10-8 will be accepted.

Sample Input
1 2
2 5
9 9
Sample Ouput
3.464101615137754587 3.464101615137754587 3.464101615137754587
6 8 10
NO Solution!

考虑有解的情况:
我们可以构造一个等腰三角形,然后二分底边的长度,再通过等面积法算出内切圆半径,与题目所给进行比较,缩小范围

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 800005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-10
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

double tpow(double a) {
    return a * a;
}


int main()
{
    //ios::sync_with_stdio(false);
    double r, R;
    while (cin >> r >> R) {
        if (r*2 > R) {
            cout << "NO Solution!" << endl;
        }
        else {
            double lft = 0, rig = sqrt(3.0)*R;
            while (rig - lft > eps) {
                double mid = (rig + lft) / 2.0;
                double x = sqrt(tpow(sqrt(tpow(R) - tpow(mid / 2.0)) + R) + tpow(mid / 2.0));
                double area = (sqrt(tpow(R) - tpow(mid / 2.0)) + R)*mid / 2.0;
                double pp = (x + x + mid) / 2.0;
                double rr = area / pp;
                if (rr - r > eps)rig = mid;
                else lft = mid;
            }
            double x = sqrt(tpow(sqrt(tpow(R) - tpow(lft / 2.0)) + R) + tpow(lft / 2.0));
            double y = lft;
            printf("%.15f %.15f %.15f\n", x, x, y);
        }
    }
}
网友评论