PAT B (Basic Level) exercises triangle

Subject description
given three sides, determine what you can not form a triangle.

Description Input:
Input of data comprising a plurality of sets, each set of data comprising three positive integers a, b, c (1≤a, b, c≤10 ^ 100).

Output Description:
corresponding to each set of data, if they can form a triangle, the output "Yes"; otherwise, outputs "No".

Input example:

1 2 3
2 2 2

Output example:

No
Yes

solution question \ Color {blue} problem-solving ideas:
to set up a triangle determine the conditions, learned a little elementary school mathematics knows任意两条边之和大于第三边, but there is another way to determine任意两边之差小于第三边, in fact, both the core idea is the same, just different forms of judgment, for examplea + b > c, can be calleda > c - borb > c - a.

Since the data is relatively large, here we use the 任意两边之差小于第三边way of judgment.
Note: Due to a > c - bthe b > c - aequivalent of three conditions can not write so repeat.

\ Color {blue} code for:

#include <iostream>
using namespace std;

int main() {
	//注意输入范围是10^100级别,所以int、long long类型会超出
    double a = 0, b = 0, c = 0;
    //scanf返回值为正确输入数据的变量个数,当一个变量都没有成功获取数据时,此时返回-1
    while (scanf("%lf %lf %lf", &a, &b, &c) != - 1) {
        //a < b + c, b < a + c, c < b + a任意两边之和大于第三版(任意两边之差小于第三边,注意别写重复了)
        //a > c - b 与 b > c - a 都是判断 a + b > c
        if (a - b < c && b - c < a && c - a < b) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }
    }
    return 0;
}

Here Insert Picture Description

Released 1005 original articles · won praise 269 · views 220 000 +

Guess you like

Origin blog.csdn.net/qq_41855420/article/details/104699291