处理数字

来源:互联网 发布:软件企业会计科目设置 编辑:程序博客网 时间:2024/05/10 16:25
H - Numbers

Description

There are n numbers ${A}_{1},{A}_{2}....{A}_{n}$,your task is to check whether there exists there different positive integers i, j, k ($1\leq i , j , k \leq n$) such that ${A}_{i}-{A}_{j}={A}_{k}$
 

Input

There are multiple test cases, no more than 1000 cases.
First line of each case contains a single integer n.$(3\leq n\leq 100)$.
Next line contains n integers ${A}_{1},{A}_{2}....{A}_{n}$.$(0\leq {A}_{i}\leq 1000)$
 

Output

For each case output "YES" in a single line if you find such i, j, k, otherwise output "NO".
 

Sample Input

33 1 231 0 241 1 0 2
 

Sample Output

YESNOYES
题意:输入几个数,如果这几个数字中俩个数之差为第三个数,即a[i]+a[i+1]==a[i+2,]就输出YES,否则输出NO。
思路:将这几个数由大到小排序,采用暴力的方法用三层循环,第一层为i,第二层为i+1,第三层为i+2,逐个扫描,如果a[i]+a[i+1]==a[i+2]就跳出循环,在这里还得用个标记变量,这是经久不变的极其常用的方法,所以定要记住!!!!!!!
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int a[2000];   //这里一定要开大,不然会超时的
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int h=0;   //此处标记变量,作用巨大
        for(int i=0; i<n; i++)     //三层循环,暴力解决
        {
            for(int j=i+1; j<n; j++)
            {
                for(int k=j+1; k<n; k++)
                {
                    if(a[i]+a[j]==a[k])
                    {
                        h=1;
                        break;
                    }
                }
            }
        }
        if(h==1)printf("YES\n");
        else printf("NO\n");
    }
}


0 0
原创粉丝点击