HDU5655 CA Loves Stick【水题】

CA Loves Stick

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3281 Accepted Submission(s): 899

Problem Description
CA loves to play with sticks.
One day he receives four pieces of sticks, he wants to know these sticks can spell a quadrilateral.
(What is quadrilateral? Click here: https://en.wikipedia.org/wiki/Quadrilateral)

Input
First line contains T denoting the number of testcases.
T testcases follow. Each testcase contains four integers a,b,c,d in a line, denoting the length of sticks.
1≤T≤1000, 0≤a,b,c,d≤263−1

Output
For each testcase, if these sticks can spell a quadrilateral, output "Yes"; otherwise, output "No" (without the quotation marks).

Sample Input
2
1 1 1 1
1 1 9 2

Sample Output
Yes
No

Source
BestCoder Round #78 (div.2)

问题链接HDU5655 CA Loves Stick
问题简述
    输入四条边,判定是否能够构成四边形。
问题分析
    关键点是边长不能够为0,同时三条短边的和要大于最长边。边长虽然都是2进制63位的,但是3个数的和有可能超出long long类型的值范围,所以写比较式需要注意。写成a[3] - a[0] - a[1] < a[2]是没有问题的,但是写成a[3] < a[0] + a[1] + a[2]则可能发生溢出。
程序说明:(略)
参考链接:(略)
题记:计算时需要注意值的范围。

AC的C语言程序如下:

/* HDU5655 CA Loves Stick */

#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int main()
{
    int t;
    long long a[4];
    scanf("%d", &t);
    while(t--) {
        scanf("%lld%lld%lld%lld", &a[0], &a[1], &a[2], &a[3]);

        sort(a, a + 4);

        printf("%s\n", a[0] != 0 && a[3] - a[0] - a[1] < a[2] ? "Yes" : "No");
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10206845.html
CA