Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <iostream> using namespace std; #define MAXSIZE 50000 int countch(int a[], int n, bool up) // up: na pocz�ku w g�r� (true) albo w d� (false) { int result = 0, prev = a[0]; int nm1 = n - 1; for (int i = 1; i < nm1; i++) { if (up && prev >= a[i]) // Ma by� w g�r�, a nie jest { prev = (prev > a[i + 1] ? prev : a[i + 1]) + 1; result++; } else if (!up && prev <= a[i]) { prev = (prev < a[i + 1] ? prev : a[i + 1]) - 1; result++; } else prev = a[i]; up = !up; } // Czy ostatni trzeba zmieni�? if ((up && prev >= a[nm1]) || (!up && prev <= a[nm1])) result++; return result; } int main() { int n, minchnum; int a[MAXSIZE]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int chnum1 = countch(a, n, true); int chnum2 = countch(a, n, false); minchnum = (chnum1 < chnum2 ? chnum1 : chnum2); cout << minchnum << endl; // system("pause"); }
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 | #include <iostream> using namespace std; #define MAXSIZE 50000 int countch(int a[], int n, bool up) // up: na pocz�ku w g�r� (true) albo w d� (false) { int result = 0, prev = a[0]; int nm1 = n - 1; for (int i = 1; i < nm1; i++) { if (up && prev >= a[i]) // Ma by� w g�r�, a nie jest { prev = (prev > a[i + 1] ? prev : a[i + 1]) + 1; result++; } else if (!up && prev <= a[i]) { prev = (prev < a[i + 1] ? prev : a[i + 1]) - 1; result++; } else prev = a[i]; up = !up; } // Czy ostatni trzeba zmieni�? if ((up && prev >= a[nm1]) || (!up && prev <= a[nm1])) result++; return result; } int main() { int n, minchnum; int a[MAXSIZE]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int chnum1 = countch(a, n, true); int chnum2 = countch(a, n, false); minchnum = (chnum1 < chnum2 ? chnum1 : chnum2); cout << minchnum << endl; // system("pause"); } |