1.题目描述:
2.题意概述:
给你一个数组,问是否能找到元素和不为 0 的子数组。
3.解题思路:
只要原数组不全为 0 就肯定 YES 啊,然后不为 0 的元素自己就是一个(只存在一个元素)数组,碰到 0 的元素,就跟下一个不为的 0 的元素合一块作为一个数组就行了。
有些题其实想明白核心代码就几行orz
4.AC代码:
#include <stdio.h>
#define N 101
using namespace std;
int a[N];
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int flag = 0, sum = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
if (a[i] != 0 && flag == 0)
flag = i;
}
if (flag == 0)
{
puts("NO");
continue;
}
puts("YES");
if (sum == 0)
printf("2\n%d %d\n%d %d\n", 1, flag, flag + 1, n);
else
printf("1\n%d %d\n", 1, n);
}
return 0;
}