首页 > ACM题库 > HDU-杭电 > hdu 2393 Higher Math-几何[解题报告]C++
2014
01-05

hdu 2393 Higher Math-几何[解题报告]C++

Higher Math

问题描述 :

You are building a house. You’d prefer if all the walls have a precise right angle relative to the ground, but you have no device to measure angles. A friend says he has a great idea how you could ensure that all walls are upright: All you need to do is step away a few feet from the wall, measure how far away you are from the wall, measure the height of the wall, and the distance from the upper edge of the wall to where you stand. You friend tells you to do these measurements for all walls, then he’ll tell you how to proceed. Sadly, just as you are done, a timber falls on your friend, and an ambulance brings him to the hospital. This is too bad, because now you have to figure out what to do with your measurements yourself.

Given the three sides of a triangle, determine if the triangle is a right triangle, i.e. if one of the triangle’s angles is 90 degrees.

输入:

The inputs start with a line containing a single integer n. Each of the n following lines contains one test case. Each test case consists of three integers 1 <= a, b, c <= 40000 separated by a space. The three integers are the lengths of the sides of a triangle.

输出:

The inputs start with a line containing a single integer n. Each of the n following lines contains one test case. Each test case consists of three integers 1 <= a, b, c <= 40000 separated by a space. The three integers are the lengths of the sides of a triangle.

样例输入:

2
36 77 85
40 55 69

样例输出:

Scenario #1:
yes

Scenario #2:
no


题意:输入三角形三边长,判断是否是直角三角形。

mark:勾股定理,最大边40000,运算中间数最大是3.2*10^9,比int的上界2.2*10^10小,因此不用担心溢出。

代码:

# include <stdio.h>


int main ()
{
    int T, a, b, c ;
    int nCase = 1 ;
    scanf ("%d", &T) ;
    while (T--)
    {
        scanf ("%d%d%d", &a, &b, &c) ;
        if (a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a)
            printf ("Scenario #%d:\nyes\n\n", nCase++) ;
        else
            printf ("Scenario #%d:\nno\n\n", nCase++) ;
    }
    return 0 ;
}

转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/06/2315250.html


  1. 给你一组数据吧:29 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1000。此时的数据量还是很小的,耗时却不短。这种方法确实可以,当然或许还有其他的优化方案,但是优化只能针对某些数据,不太可能在所有情况下都能在可接受的时间内求解出答案。

  2. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。