题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5522
Description
给n个数A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A1,A2....An,从中选3个位置不同的数A,B和C,问是否有一种情况满足A-B=C.
Input
There are multiple test cases, no more than 1000 cases.
First line of each case contains a single integer n.(3≤n≤100).
Next line contains n integers A1,A2....An.(0≤Ai≤1000)
Output
For each case output "YES" in a single line if you find such i, j, k, otherwise output "NO"
Sample Input
3
3 1 2
3
1 0 2
4
1 1 0 2
3 1 2
3
1 0 2
4
1 1 0 2
Sample Output
YES
NO
YES
先排序,再比较
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
return *(int*)a-*(int *)b;
}
int main()
{
int n,i,j,k,a[105],p,flag,m;
while(scanf("%d",&n)!=EOF)
{
flag=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(a[0]),cmp);
for(j=0;j<n-2;j++)
{
for(m=1;m<n-1-j;m++)
{
p=a[j]+a[j+m];
for(k=n-1;k>j+m;k--)
{
if(a[k]==p)
flag=1;
}
}
}
if(flag==0)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}