Geometry Made Simple

Geometry Made Simple

时间: 1ms        内存:64M

描述:

Mathematics can be so easy when you have a computer. Consider the following example. You probably know that in a right-angled triangle, the length of the three sides a, b, c (where c is the longest side, called the hypotenuse) satisfy the relation a*a+b*b=c*c. This is called Pythagora's Law.

Here we consider the problem of computing the length of the third side, if two are given.

输入:

The input contains the descriptions of several triangles. Each description consists of a line containing three integers a, b and c, giving the lengths of the respective sides of a right-angled triangle. Exactly one of the three numbers is equal to -1 (the 'unknown' side), the others are positive (the 'given' sides).

A description having a=b=c=0 terminates the input.

输出:

For each triangle description in the input, first output the number of the triangle, as shown in the sample output. Then print "Impossible." if there is no right-angled triangle, that has the 'given' side lengths. Otherwise output the length of the 'unknown' side in the format "s = l", where s is the name of the unknown side (a, b or c), and l is its length. l must be printed exact to three digits to the right of the decimal point.

Print a blank line after each test case.

示例输入:

3 4 -1
-1 2 7
5 -1 3
0 0 0

示例输出:

Triangle #1
c = 5.000

Triangle #2
a = 6.708

Triangle #3
Impossible.

提示:

参考答案(内存最优[760]):

#include<stdio.h>
#include<math.h>

int main()
{
    double a,b,c,temp;
    int i=0;
    while(scanf("%lf %lf %lf",&a,&b,&c)!=EOF)
    {
        if(a==0&&b==0&&c==0)
            break;
        else if(a==-1)
            {
                i++;
                temp=c*c-b*b;
                if(temp<=0)
                    printf("Triangle #%d\nImpossible.\n\n",i);
                else
                {
                    a=sqrt(temp);
                    printf("Triangle #%d\na = %.3f\n\n",i,a);
                }
            }
        else if(b==-1)
            {
                i++;
                temp=c*c-a*a;
                if(temp<=0)
                    printf("Triangle #%d\nImpossible.\n\n",i);
                else
                    {
                        b=sqrt(temp);
                        printf("Triangle #%d\nb = %.3f\n\n",i,b);
                    }
            }
        else if(c==-1)
        {
            i++;
            temp=a*a+b*b;
            c=sqrt(temp);
            printf("Triangle #%d\nc = %.3f\n\n",i,c);
        }
    }
    return 0;
}

参考答案(时间最优[0]):

#include <stdio.h>
#include <math.h>
int main()
{
    int a,b,c,k=1;
    double m=0;
    while (scanf("%d%d%d",&a,&b,&c))
    {
        if (a==0&&b==0&&c==0)
            break;
        printf("Triangle #%d\n",k);
        if ((c<a||c<b)&&c!=-1)
            printf("Impossible.\n");
        else if (a==-1)
        {
            m=sqrt(c*c-b*b);
            printf("a = %.3lf\n",m);
        }
        else if (b==-1)
        {
            m=sqrt(c*c-a*a);
            printf("b = %.3lf\n",m);
        }
        else if (c==-1)
        {
            m=sqrt(a*a+b*b);
            printf("c = %.3lf\n",m);
        }
        printf("\n");
        k++;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注