题目描述
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
参考代码
#include<stdio.h>
int main()
{
int a[100005];
int b[100005];
int n;
int i,j;
while(scanf("%d",&n)!=EOF)
{
int s=0;
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=b[i-1]+a[i];
if(a[i]==0||b[i]==0)
{
s=1;
}
}
if(s==1)
printf("YESn");
else
{
for(i=1;i<=n;i++)
{
if(s==1)
break;
for(j=0;j<i;j++)
{
if(b[i]-b[j]==0)
{
s=1;
break;}
}
}
if(s==1)
printf("YESn");
else
printf("NOn");
}
}
return 0;
}
解析
暂无