相同数字的最大连续子序列

要在规定时间里面完成,内存不能超。

img

img


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a;
    int res = 0;
    int cnt = 1;
    cin>>a;
    int pre = a;
    while(cin>>a,a)
    {
        if(a==pre) cnt++;
        else cnt = 1;
        res = max(res,cnt);
        pre = a;
    }
    cout<<res<<'\n';
    return 0;
}

答案应该就是这,cnt代表连续的数字的个数,res随时更新答案,pre代表前一个数字,当当前数a等于前一个数pre时候,cnt加一(连续相同子序列长度加一),否则cnt为1(重新开始了一个子序列,初始长度为1)