Problem description
Given a sequence of integers, the longest continuous sequence of integers in the sequence is counted as one segment. How many segments are there in the sequence?
Input format
The first line of input contains an integer n, which represents the number of integers in the sequence.
The second line contains n integers a1, a2, …, an, representing a given sequence of numbers, and adjacent integers are separated by a space.
Output format
outputs an integer, indicating that the given sequence has multiple segments.
Sample input
8
8 8 8 0 12 12 8 0
Sample output
5
The example shows that
8 8 8 is the first paragraph, 0 is the second paragraph, 12 12 is the third paragraph, the penultimate integer 8 is the fourth paragraph, and the last 0 is the fifth paragraph.
Evaluation use case scale and agreement
1 ≤ n ≤ 1000, 0 ≤ ai ≤ 1000.
Submitted it again, but made a mistake. Cnt should start counting from 1. Through the loop, it can be seen that it is the comparison between the latter and the previous one. The first one has been compared by default, and it should be counted. Then when the next one is equal, Continue, counting only if they are not equal. At this time, the position of j is the subscript of the first number that is not equal to the previous one. The comparison of i again should start from j, but since the outer loop is once, i++, so i = j-1
#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[n];
for(int i = 0;i<n;i++){
cin >> a[i];
}
int cnt = 1;
for(int i = 0;i<n;i++){
int j;
for(j = i;j<n;j++){
if(a[j]!=a[i]){
cnt++;
break;
}
}
i = j-1;
}
cout << cnt;
return 0;
}