2173: Sum in Set

2173: Sum in Set


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s32768K1401235Standard
In this problem you will be given a set (S) of numbers. Your task is to check whether there are three numbers in S, called A, B, C, such that A+B+C=0. (A, B, C may be the same number)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值