题目:
CA Loves Stick
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2126 Accepted Submission(s): 612
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≤2e63−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)
题意:
给4条边,判断是否构成四边形。思路很简单,先排序,最小三边a+b+c>d即可。
但是注意这里的数据范围:0≤a,b,c,d≤2e63−1
首先,需要特判a, b, c, d是否小于等于0
其次,注意每一条边都有可能到long long的上界,用了unsigned long long 也只能承受一次相加。
可以变形一下:a>d-c-b
挺坑的,注意数据范围就不会WA这么多次了
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
const int maxn = 4;
unsigned long long arr[maxn];
using namespace std;
int main(){
int n;
while(cin>>n){
for(int i=0; i<n; i++){
memset(arr, 0, sizeof(arr));
cin>>arr[0]>>arr[1]>>arr[2]>>arr[3];
sort(arr, arr+4);
if(arr[0]==0||arr[1]==0||arr[2]==0||arr[3]==0) {cout<<"No"<<endl; continue;}
if(arr[0]>arr[3])
cout<<"Yes"<<endl;
else if(arr[0]+arr[1]>arr[3])
cout<<"Yes"<<endl;
else if(arr[0]+arr[1]+arr[2]>arr[3])
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
我这样,也过了(–)..