Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/262144 K (Java/Others)Total Submission(s): 88 Accepted Submission(s): 67
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
hujie
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int vis[2010];
int num[110];
int main()
{
int i,j,k=1,n;
while(scanf("%d",&n)!=EOF){
memset(vis,0,sizeof(vis));
for(i=0;i<n;++i){
scanf("%d",&num[i]);
vis[num[i]]++;
}
int flag=0;
for(i=0;i<n&&flag==0;++i){
for(j=0;j<n&&flag==0;++j){
if(i==j)continue;
if(vis[num[i]+num[j]]){
vis[num[i]]--;
vis[num[j]]--;
if(vis[num[i]+num[j]]){
flag=1;break;
}
vis[num[i]]++;
vis[num[j]]++;
}
}
}
if(flag){
printf("YES\n");
}
else {
printf("NO\n");
}
}
return 0;
}