求一组数中最大连续奇数个数的算法

求一组数中最大连续奇数个数的算法
如 1 2 3 0 8 7 5 3 2
最大连续奇数个数是3个 7 5 3
怎么实现?
arlenefly 1年前 已收到1个回答 举报

silence_xy 幼苗

共回答了16个问题采纳率:87.5% 举报

int maxL =0;
int thisL =0;
int input[] = {1,2,3 }; // 输入的整数
for ( int idx =0; idx < input.length; idx++)
{
if ( input[idx] 是奇数)
thisL = thisL + 1;
else
{
maxL = max{ maxL,thisL};
thisL=0;
}
}
maxL= max{ maxL,thisL};
// 最大连续奇数个数= maxL
不清楚 请继续问.

1年前 追问

4

arlenefly 举报

非常感谢,但是数据比较大情况下,放在文件里要怎么操作呢

举报 silence_xy

那 input[] 从读文件获得。 如果很大,你还可以 修改下: else { maxL = max{ maxL, thisL}; thisL=0; } 改成: else { if (thisL >0) { maxL = max{ maxL, thisL}; thisL=0; } }
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.029 s. - webmaster@yulucn.com