fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. ///-------------------------------------------------------------------------------------
  5.  
  6. int returnArrayElement(int arr[], unsigned int n){
  7. double sum; //suma jest typu double by przy obliczaniu sredniej nie dostac liczby calkowitej
  8. for(int i=0; i<n; i++)
  9. sum+=arr[i];
  10. double average = sum/n;
  11. //std::cout<<average<<std::endl;
  12.  
  13. int theSmallDiff = 0; //indeks tablicy najblizszy sredniej (poxczątkowo zerowy)
  14.  
  15. for(int i=1; i<n; i++){
  16. if(std::abs(average-arr[i]) < std::abs(average-arr[theSmallDiff]))//wyszukujemy nowe minimum
  17. theSmallDiff = i;
  18. }
  19. //std::cout<<arr[theSmallDiff]<<std::endl;
  20. return arr[theSmallDiff];
  21. }
  22.  
  23. ///----------------------------------------------------------------------------------------
  24.  
  25. int main(){
  26. int t;
  27. std::cin>>t;
  28.  
  29. for(int i=0; i<t; i++){
  30. int n;
  31. std::cin>>n;
  32.  
  33. int* arr = new int[n];
  34.  
  35. for(int i=0; i<n; i++){
  36. std::cin>>arr[i];
  37. }
  38.  
  39. std::cout<<returnArrayElement(arr,n)<<std::endl;
  40.  
  41. delete[] arr;
  42. }
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 4184KB
stdin
3
4 1 2 3 4 
4 4 3 2 1
4 0 3 2 4
stdout
1
3
3