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.
Input
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.
Output
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.
Sample Input
3 4 -1
-1 2 7
5 -1 3
0 0 0
Sample Output
Triangle #1
c = 5.000
Triangle #2
a = 6.708
Triangle #3
Impossible.
题意:
给出三个数,依次代表给出的三角形的三条边。其中第三条边代表最长的那条边,如果某条边输入为-1,那么这条边长度未知,如果可以组成直角三角形,则求出这条边,输出,否则输出相应的提示。
这个题比较简单,只要能把题意看明白,就很好处理了,最直接的方法是分情况讨论,这个比较好理解,这个题,只需要第三条边大于另外一条边就行了。
如果满足直角三角形,然后对应的用勾股定理,求出另外一条边的长度,方法比较简单。
/*
时间:201507211004
功能:判断是否构成直角三角形,若能,求出另外一条边。
*/
#include<stdio.h>
#include<math.h>
int main()
{
int i=0;double a,b,c;
while(scanf("%lf%lf%lf",&a,&b,&c),a||b||c)
{
++i;
if(c<0)
{
c=sqrt(a*a+b*b);
printf("Triangle #%d\nc = %.3lf\n\n",i,c);
}
else if(a<0&&c>b)
{
a=sqrt(c*c-b*b);
printf("Triangle #%d\na = %.3lf\n\n",i,a);
}
else if(b<0&&c>a)
{
b=sqrt(c*c-a*a);
printf("Triangle #%d\nb = %.3lf\n\n",i,b);
}
else
{
printf("Triangle #%d\nImpossible.\n\n",i);
}
}
return 0;
}