#include <iostream> #include <vector> using namespace std; int main() { int n, a, prevNum, status, result = 0; cin >> n; vector<int> arr; for (int i = 0; i < n; i++) { cin >> a; arr.push_back(a); } //-1 malejąca // 0 poprzednie złe // 1 rosnąca if (arr[0] > arr[1]) status = -1; else if (arr[0] < arr[1]) status = 1; else { status = 0; result++; } for (int i = 2; i < n; i++) { prevNum = arr[i - 1]; if (status == 0) { if (prevNum > arr[i]) status = -1; else if (prevNum < arr[i]) status = 1; else { status = 0; result++; } } else if (status == -1) { if (prevNum < arr[i]) status = 1; else { status = 0; result++; } } else { if (prevNum > arr[i]) status = -1; else { status = 0; result++; } } } cout << result << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <vector> using namespace std; int main() { int n, a, prevNum, status, result = 0; cin >> n; vector<int> arr; for (int i = 0; i < n; i++) { cin >> a; arr.push_back(a); } //-1 malejąca // 0 poprzednie złe // 1 rosnąca if (arr[0] > arr[1]) status = -1; else if (arr[0] < arr[1]) status = 1; else { status = 0; result++; } for (int i = 2; i < n; i++) { prevNum = arr[i - 1]; if (status == 0) { if (prevNum > arr[i]) status = -1; else if (prevNum < arr[i]) status = 1; else { status = 0; result++; } } else if (status == -1) { if (prevNum < arr[i]) status = 1; else { status = 0; result++; } } else { if (prevNum > arr[i]) status = -1; else { status = 0; result++; } } } cout << result << endl; } |