题目:
解答:
#include <iostream> #include <vector> using namespace std; int main() { int nNum,nArrayElement; vector<int> vecArray; cin >> nNum; for (int i = 0; i < nNum; i++) { cin >> nArrayElement; vecArray.push_back(nArrayElement); } int nNumofInterval = 1; for (int i = 0; i < nNum-1; i++) { //如果前后相差不等于1,则自增 if (vecArray[i+1] - vecArray[i] != 1) { nNumofInterval++; } } cout << nNumofInterval; return 0; }