基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
![](https://file.51nod.com/images/icon/star.png)
![](https://file.51nod.com/images/icon/plus.png)
给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No"。
Input
第1行,1个数N,N为数组的长度(4 <= N <= 1000) 第2 - N + 1行:A[i](-10^9 <= A[i] <= 10^9)
Output
如果可以选出4个数,使得他们的和为0,则输出"Yes",否则输出"No"。
Input示例
5 -1 1 -5 2 4
Output示例
Yes
#include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> typedef long long int ll; using namespace std; ll a[1001]; int main() { ll n,i,j,l,r; scanf("%lld",&n); for(i=0; i<=n-1; i++) scanf("%lld",&a[i]); sort(a,a+n); for(i=0; i<=n-1; i++) for(j=i+1; j<=n-1; j++) { int ans1=-a[i]-a[j]; l=j+1; r=n-1; while(l<r) { if(a[l]+a[r]==ans1) { printf("Yes "); return 0; } else if(a[l]+a[r]>ans1) r--; else if(a[l]+a[r]<ans1) l++; } } printf("No "); return 0; }