2173: Sum in Set
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 32768K | 1401 | 235 | Standard |
Input
There are more than one test cases in the input file. Each test case contains two lines. The first line have just one positive integer N (1 <= N <= 1000). The second line have N numbers seperated by spaces. The last case contains N=0 which you should not process.Output
For each test case just print "yes" if there are A+B+C=0 or "no" if there are not.Sample Input
5 0 1 2 3 4 3 -1 2 4 2 1 2 0
Sample Output
yes yes no
Problem Source: coldcpp
#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{ return *(double *)a-*(double *)b;}
int main()
{ double a[1000];
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
int i,temp;
for(i=0;i<n;i++)
scanf("%lf",&a[i]);
qsort(a,n,sizeof(a[0]),cmp);
int head,tail;
bool flag=false;
for(i=0;i<n;i++)
{ temp=a[i];
head=0;
tail=n-1;
while(head<=tail)
{
if((temp+a[head]+a[tail])==0)
{ flag=true; break;
}
else if(temp+a[head]+a[tail]<0)
head++;
else tail--;
}
if(flag)
break;
}
if(flag)
printf("yes/n");
else printf("no/n");
}
return 0;
}