Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/262144 K (Java/Others)Total Submission(s): 1123 Accepted Submission(s): 614
Problem Description
There are n numbers
A1,A2....An
,your task is to check whether there exists there different positive integers i, j, k (
1≤i,j,k≤n
) such that
Ai−Aj=Ak
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)
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
Sample Output
YES NO YES
Source
Recommend
这里贴一个o(n2)的代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[11000],i,j,k,l,m,n,p,b[11000],vist[11000];
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(vist,0,sizeof(vist));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
vist[a[i]]++;
}
sort(a,a+n);
int cnt=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
vist[a[i]]--;
vist[a[j]]--;
if(vist[a[i]+a[j]])
cnt=1;
vist[a[i]]++;
vist[a[j]]++;
}
if(cnt)
printf("YES\n");
else
printf("NO\n");
}
}