Introduction to the C + + algorithm race Classic Page16 exercise 1-6 triangle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.