1. 程式人生 > >HDU5655 CA Loves Stick【水題】

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;
}