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”这种情况的处理。

时间: 2024-08-24 23:09:24

SGU[151] Construct a triangle的相关文章

CF 6A Triangle (判断能否构成三角形)

Triangle Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on CodeForces. Original ID: 6A 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None None Graph Theory      2-SA

sgu100~199题解

老东西了..发上来吧.. Sgu题解系列  南开中学邹事成 100:A+B略 101:Domino 给n块多米诺骨牌,每张骨牌两端各有从1到6的一个数字,现在要把这些骨牌排成一列,使相邻的两块骨牌相对的面所写的数字一样. 可以把每一块多米诺骨牌想象成一条边,把面上写的数字抽象成点,比如一块骨牌正面写的1反面写的2就想象成连了一条从1到2的边,那么这就是求一条有重边的欧拉回路了,dfs一下即可. 102:Coprimes给定n求从1到n中与n互质的数的个数. 可以把n质因数分解后直接代入欧拉函数.

In-circles Again(简单几何)

In-circles Again Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 4272 In the figure below you can see triangle ABC and its in-circle (Circle that touches all the sides of a triangl

2017-4-17-Train:Codeforces Beta Round #6 (Div. 2 Only)

A. Triangle(阅读题 + next_premutation) Johnny has a younger sister Anne, who is very clever and smart. As she came home from the kindergarten, she told his brother about the task that her kindergartener asked her to solve. The task was just to construct

cf682E Alyona and Triangles

You are given n points with integer coordinates on the plane. Points are given in a way such that there is no triangle, formed by any three of these n points, which area exceeds S. Alyona tried to construct a triangle with integer coordinates, which

C语言与VT100控制码编程

C语言与VT100控制码编程 声明: 1. 如果您打算阅读本文,希望您已经了解过C语言的基本语法,本文不对C语言的基本语法进行说明,因为那些东西几乎唾手可得; 2. 本文在vim中编辑,请尽量是用vim进行阅读,因为有不对齐的现象; 3. 本人强烈建议您先编译,运行本文最后提供的sinDemo源代码,再看本文的正文,因为您看了运行效果,您就知道本人为什么要写这篇文章; \\\\\\\\\\\\--*目录*--/////////// | 一. 需求背景 | | 二. VT100控制码是什么 | |

OpenCV Tutorials —— Creating Widgets

Explanation Extend Widget3D class to create a new 3D widget. Assign a VTK actor to the widget. Set color of the widget. Construct a triangle widget and display it in the window. Code #include <opencv2/viz/vizcore.hpp> #include <opencv2/viz/widget

EECE 1080C / Programming for ECE

EECE 1080C / Programming for ECESummer 2019Laboratory 10: C++ Operator OverloadingPlagiarism will not be tolerated:all students who share files will receive a 100% penalty on this assignmentTopics covered: Classes FunctionsObjective: To practice the

SGU 299.Triangle

题意: 给出n(<=1000)条线段的长度ai(<=10^500),输出任意三条能组成三角形的边.没有输出3个0. Solution: 简单题.只是要处理高精度. java大法好. import java.util.*; import java.math.*; public class Solution { public static void main(String[] args){ Scanner cin=new Scanner(System.in); int n=cin.nextInt(