fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. long long kth(int *arr, int l,int r,int k)
  6. {
  7. long long x = arr[(l+r) / 2];
  8. int i=l,j=r;
  9. while(i<=j)
  10. {
  11. while(arr[i] < x) i++;
  12. while(arr[j] > x) j--;
  13.  
  14. if(i<=j)
  15. {
  16. std::swap(arr[i],arr[j]);
  17. i++;
  18. j--;
  19. }
  20. }
  21. if(l<=k && k<=j)
  22. return kth(arr, l,j,k);
  23. if( i<=k && k<=r)
  24. return kth(arr, i,r,k);
  25. return arr[k];
  26. }
  27.  
  28. int main(){
  29. int i, n, p, q;
  30. int *seq;
  31.  
  32. scanf("%d", &n);
  33. printf("n = %d\n", n);
  34. seq = (int *)malloc(n*sizeof(int));
  35.  
  36. for(i = 0; i < n; i++){
  37. scanf("%d", *(seq + i));
  38. printf("p = %d\n", n);
  39. }
  40.  
  41.  
  42. if(n & 0x1){
  43. p = kth(seq, 0, n, (n + 1) / 2);
  44. printf("%d\n", p);
  45. }else{
  46. double a, b;
  47. a = kth(seq, 0, n, n/2);
  48. b = kth(seq, 0, n, (n/2) + 1);
  49. printf("%.1f\n", (a + b) / 2);
  50. }
  51.  
  52. scanf("%d", &n);
  53. return 0;
  54. }
Runtime error #stdin #stdout 0s 3460KB
stdin
5
5
4
3
2
1
stdout
Standard output is empty