CCF认证真题-(201509-1)-数列分段
思路: 变量pre记录前一个数, cnt记录段数. 如果发现当前数与当前pre的值不同,段数+1.
#include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int pre = -1, cnt = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (x != pre) cnt++; pre = x; } cout << cnt << endl; return 0; }