#include<iostream>
using namespace std;
int main()
{
int n;
int pre = -1, sum = 0, current;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> current;
if (current != pre) {
pre = current;//交错序列没要求连续,实质去重
sum++;
}
}
cout << sum << endl;
return 0;
}
若求连续交错序列,代码如下:
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int pre = -1;
int current, sum=0,max=0;
for (int i = 0; i < n; i++)
{
cin >> current;
if (current != pre)
{
pre=current;
sum++;
}
else
{
sum = 1;
}
if (sum>max)
{
max=sum;
}
}
cout << max << endl;
return 0;
}
运行结果:
8
1 1 0 0 1 1 0 0
2