zoukankan      html  css  js  c++  java
  • SGU[151] Construct a triangle

    Description

    描述

    Find coordinates of any triangle ABC if it is know that |AB|=c, |AC|=b, |AM|=m, AM is a median of triangle.

    找到任何一个三角形ABC,使得它满足|AB| = c,|AC| = b,|AM| = m,其中AM为三角形的中线。

     

    Input

    输入

    There are three real numbers in input: c, b, m (0<c,b,m<=10^3) separated by a space. Length of the fractional part of each number is not greater than 2 digits.

    输入中包含三个实数:c, b, m (0 < c, b, m <= 10^3),以空格分隔。每个数字的小数部分的长度不超过2位。


    Output

    输出

    If solution exists, write three lines. Write coordinates of point A to first line, coordinates of B to second line and coordinates of C to third line. Separate numbers by a space; absolute value of each coordinate must not exceed 10^4. Write numbers with 5 digits after decimal point. If there is no solution, write "Mission impossible"

    如果存在方案,输出三行。第一行为点A的坐标,第二行为点B的坐标,第三行为点C的坐标。数字之间以空格分隔。每个坐标的绝对值不能超过10^4。保留5位小数。如果不存在这样的方案,输出“Mission impossible”。


    Sample Input

    样例输入

    5 5 3


    Sample Output

    样例输出

    0.00000 3.00000

    -4.00000 0.00000

    4.00000 0.00000

     

    Analysis

    分析

    解析几何的题目,因为是任意输出一个三角形ABC,为了简化计算,我们不妨令点A为坐标原点,即A(0, 0)。

    同时,我们可以令点B在x轴上,即B(c, 0)。

    这样,问题就转化成了求解点C的坐标了。根据中学有关解析几何的知识,我们可以得出下面的求解过程:

    设C(x, y),则M((x + c) / 2, y / 2)。得方程组:

    x^2 + y^2 = b^2                ( 1 )

    ((x + c) / 2)^2 + (y / 2)^2 = m^2        ( 2 )

    将上面两个式子联立,化简即可得到:x = (4 * m^2 - b^2 - c^2) / (2 * c),则y = sqrt(b^2 - x^2)。

    接下来要判断是否有解,一种方法是根据上面两个方程推导有解的条件,另一种方法是直接判断y^2是否大于等于0。这里我们采用第二种方法,因为不需要额外推导公式,利用已有的结果就可以得出我们需要的答案。

    要特别注意的是,在这里,很可能出现“-0.00000”的情况,对于这种情况,我们需要进行特殊处理。使得它等于0。

     

    Solution

    解决方案 

    #include <iostream>
    #include <iomanip>
    #include <math.h>
    
    using namespace std;
    
    int Check(double x);
    
    int main()
    {
    	double c, b, m;
    	while(cin >> c >> b >> m)
    	{
    		double x = (4 * m * m - b * b - c * c) / (2 * c), y = b * b - x * x;
    		if(Check(y) >= 0)
    		{
    			if(Check(x) == 0) { x = 0; }
    			if(Check(y) == 0) { y = 0; }
    			cout << fixed << setprecision(5) << 0.0 << " " << 0.0 << endl; 
    			cout << fixed << setprecision(5) << c << " " << 0.0 << endl; 
    			cout << fixed << setprecision(5) << x << " " << sqrt(y) << endl; 
    		}
    		else { cout << "Mission impossible" << endl; }
    	}
    	return 0;
    } 
    
    int Check(double x)
    {
    	if(fabs(x) < 1E-9) { return 0; }
    	else { return x > 0 ? 1 : -1; }
    }
    

      

    本来在刷小白书的课后习题,但是记到简单的链表和堆栈让我WA了很久,所以就去SGU上找了几道题目刷刷。

    做题目的时候一直遇不到AC很容易打击积极性。

    这道题目主要在于推到公式,以及注意对于浮点数“-0.00000”这种情况的处理。

  • 相关阅读:
    超全面的vue.js使用总结
    Python3 [字典】类型 学习笔记
    Python3 [集合]类型 学习笔记
    Python 希尔排序法
    Python 堆排序法
    Python 归并排序法
    Python 冒泡排序法
    Python 选择排序法
    Python 快速排序法(转)
    Python 插入排序法
  • 原文地址:https://www.cnblogs.com/Ivy-End/p/4297413.html
Copyright © 2011-2022 走看看