c++连续的最长偶数序列?

描述
从键盘读入n个整数,请问这n个整数中,连续的偶数最多有多少个?
输入
第1行输入一个整数n(n≤100)

第2行读入n个整数,数字之间用空格隔开

输出
输出一个整数,代表连续偶数的最长序列有多长
样例输入
10
1 2 8 47 2 3374 944 992 198 100
样例输出
6

代码如下,如有帮助,请采纳一下,谢谢。

#include <iostream>
using namespace std;
int main()
{
    int n,i;
    int x;
    int maxn = 0;
    int t = 0;

    cin >> n;
    for (i = 0;;i++)
    {
        if(i == n)
        {
            if(t > maxn)
                maxn = t;

            break;
        }
        cin >> x;
        if(x%2 == 0)
            t++;
        else
        {
            if(t > maxn)
                maxn = t;
            t = 0;
        }
    }
    cout << maxn << endl;
    return 0;
}

for循环从头到尾一遍即可。int一个count和count1,每次到新的偶数序列就count = max(count1,count);count1是上一个偶数序列的长度,count是到目前为止最长的偶序列长度

供参考:

#include<stdio.h>

int main()
{
    int n, i, cnt=0,max_l=0;
    scanf("%d",&n);
    while (n > 0) {
        scanf("%d",&i);
        if (i % 2 == 0){
                cnt++;
        }else {
            if (cnt > max_l)
                max_l = cnt;
            cnt = 0;        
        }
        if (n == 1){
            if (cnt > max_l)
                max_l = cnt;
        }
        n--;
    }
    printf("%d\n",max_l);
}

主要的算法,将输入的数据存入数组arr中,然后遍历数组统计最大连续值
以下是Java主要算法代码,亲测有效~ C++稍微改写即可

        int max = 0; // 初始化
        int index = 0;//计算每次最大连续的次数
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                index++;
            } else {
                index = 0;//遇到奇数,统计重复为0
            }
            max = index > max ? index : max; //比较max和index,获取最大值
        }