Advertisement
Toliak

pureC4

Oct 27th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Отладка
  4.  
  5. int array[] = {1, 2, 3, 4, 5, 3, 2, -5};
  6.  
  7. int less(unsigned long i, unsigned long j)
  8. {
  9.     return array[i] < array[j];
  10. }
  11.  
  12. unsigned long peak(unsigned long, int (*)(unsigned long, unsigned long));
  13.  
  14. int main()
  15. {
  16.     unsigned long i = peak(8, less);
  17.     if ((i == 0 || array[i] >= array[i - 1]) && (i == 7 || array[i] >= array[i + 1])) {
  18.         printf("CORRECT\n");
  19.     } else {
  20.         printf("WRONG\n");
  21.     }
  22.     return 0;
  23. }
  24.  
  25. // Решение
  26.  
  27. unsigned long peak(unsigned long nel, int (*less)(unsigned long, unsigned long))
  28. {
  29.     if (!less(0, 1)) {
  30.         return 0;
  31.     } else if (!less(nel - 1, nel - 2)) {
  32.         return nel - 1;
  33.     }
  34.  
  35.     for (unsigned long i = 1; i < nel - 1; i++) {
  36.         if (!less(i, i - 1) && !less(i, i + 1))
  37.             return i;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement