Title: Input Triangle 3 Edge length value (both are positive integers), determine whether it can be right triangle 3 sides long. If so, the output is yes, cannot output no, and if it cannot form a triangle, the output is not a triangle
Analyze the conditions that can form triangles: the sum of the two sides is greater than the third side, and the difference between the two is less than the third.
Re-analysis can become right triangle conditions: A2+B2=C2.
(1) Declare the triangular three-side variable a,b,c, and enter A,b,c:
int a,b,c; // triangular three-side
scanf ("%d%d%d", &a,&b,&c);
(2) First determine whether it is a triangle:
if (a+b>=c&&a+c>=b&&b+c>=a&&a-b<=c&&a-c<=b&&b-c<=a)
(3) If it is a triangle, determine whether it is right triangle:
if (a*a+b*b==c*c| | a*a+c*c==b*b| | B*B+C*C==A*A)
(4) According to the respective requirements of the output can be respectively.
Full code:
//Copyright belongs to AbelIvan7 http://www.cnblogs.com/Oswald/all, qq:2902408434
//P16 Exercise 1-6 Triangle#include <cstdio>intA,b,c;//triangular three-sideintMain () {scanf ("%d%d%d",&a,&b,&c); if(a+b>=c&&a+c>=b&&b+c>=a&&a-b<=c&&a-c<=b&&b-c<=a) {if(a*a+b*b==c*c| | a*a+c*c==b*b| | b*b+c*c==a*a) printf ("Yes"); Elseprintf ("No"); } Elseprintf ("Not a triangle"); return 0; }
Introduction to the C + + algorithm race Classic Page16 exercise 1-6 triangle