题解 | #乘积为正数的最长连续子数组#
乘积为正数的最长连续子数组
http://www.nowcoder.com/practice/0112b9b5a09048d89309f55ea666db91
- 同时保存最长负值数组与最长正值数组长度,分别为n,p;
- 1.当读入的为负值时,进行p与n的交换;
- 2.当读入的为正值时,p与n都加一;
- 3.当读入的为零值时,p与n都设为0;
#include<iostream>
using namespace std;
int main()
{
int n,positive = 0,negative = 0,val,len = 0;
cin >> n;
for(int i = 1;i <= n;++i){
cin >> val;
if(val > 0){
positive++;
negative = negative == 0 ? 0 : negative + 1;
}
else if(val < 0){
int temp = negative;
negative = positive + 1;
positive = temp == 0 ? 0 : temp + 1;
}
else{
positive = 0;
negative = 0;
}
len = max(len,positive);
}
cout << len ;
}