HDU 5522 Numbers 暴力

Numbers

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5522

Description

给n个数A1,A2….An{A}_{1},{A}_{2}….{A}_{n}A​1​​,A​2​​….A​n​​,从中选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

Sample Output

YES
NO
YES

HINT

 

题意

 

题解:

就n^2暴力,先排序然后从大到小枚举i,把右边的数用一个数组标记其出现过,再枚举左边的数判断其加上Ai是否出现过.

代码

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;


map<int,int> H;
int a[101];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        H.clear();
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),H[a[i]]++;
        sort(a+1,a+1+n);
        int flag = 0;
        for(int i=1;i<=n;i++)
        {
            H[a[i]]--;
            for(int j=1;j<i;j++)
                if(H[a[j]+a[i]])
                    flag = 1;
            if(flag)
                break;
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4928186.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞