Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/262144 K (Java/Others)Total Submission(s): 95 Accepted Submission(s): 72
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
出题人:先排序然后从大到小枚举i,把右边的数用一个数组标记其出现过,
再枚举左边的数判断其加上Ai是否出现过.
#include
#include
#include
#include
using namespace std;
int a[105];
int b[1001];
int main()
{
int n,i,j,t,flag;
while(~scanf("%d",&n))
{
flag=0;
for(i=0; i<=1000; i++)
b[i]=0;
for(i=0; i
=2; j--) { for(i=j-1; i>=1; i--) { int temp=a[j]-a[i]; if(temp==a[i]&&b[temp]>=2) { flag=1; break; } if(temp!=a[i]&&b[temp]>=1) { flag=1; break; } } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }