YTU.2902: H-Sum 3s

版权声明:个人博客:www.jingyile.cn 萌新发博文积累经验,欢迎各位大佬指导!!! https://blog.csdn.net/JYL1159131237/article/details/84927479

问题 H: H-Sum 3s

时间限制: 1 Sec  内存限制: 128 MB
提交: 207  解决: 50
[提交][状态][讨论版][命题人:lyh]

题目描述

You are given a number sequence a1,a2,a3...,an , your task is to find if there is a pair of interger (i,j) that ai+a(i+1)+..+aj equals to 0 and i<=j;

输入

Input consists of multiple test cases. For each case, the first line of input contains an integer n, the next line follows n integers. (n>=1 && n<=10^5 |ai|<=10^4)

输出

For each case, if there is at least one pair of integer (i,j) meet the requirement, print “YES”, otherwise print “NO” .

样例输入

5
1 2 3 4 5
5
3 4 -2 -3 1

样例输出

NO
YES

分析:求前缀和,若出现0或者有相等的情况则说明可以输出yes

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int a[maxn];
int sum[maxn];
int main()
{
    int n;
    int flag;
    while(cin>>n)
    {
    flag=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];    
        sum[i]=sum[i-1]+a[i];
        if(a[i]==0||sum[i]==0)
        flag=1;
    }
    if(flag!=1)
    {
        sort(sum,sum+n+1);
        for(int i=1;i<n;i++)
        {
            if(sum[i]==sum[i+1])
            {
            flag=1;
            break;  
            }
        }
    }
    if(flag)
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;   
    }
    return 0;   
}

猜你喜欢

转载自blog.csdn.net/JYL1159131237/article/details/84927479