fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int test,amount,a,suma,nrnearest;
  8. double average,nearest;
  9. cin>>test; // Ile testow?
  10. while(test--)
  11. {
  12. cin>>amount; //Ile liczb w tescie?
  13. int number=amount;
  14. int* tab=new int[number];
  15. double* tab1=new double[number];
  16. suma=0;
  17.  
  18. for( int i=0; i<number; i++) //zapisanie liczb do tab[]
  19. {
  20. cin>>a;
  21. tab[i]=a;
  22. suma+=a;
  23. }
  24.  
  25. average=(double)suma/number;
  26.  
  27. for (int i=0; i<number; i++) //zapisanie różnicy sredniej arytm. i każdego wyrazu do tab1[]
  28. {
  29. tab1[i]=fabs(average-tab[i]);
  30. }
  31.  
  32. nearest=tab1[0]; //tab1[0] jest różnicą najblizszą ZERU
  33. nrnearest=0; //nr liczby w tablicach
  34. for (int i=1; i<number; ++i) //Przeszukiwanie tab1[] by znaleźć numer wyrazu najbliższy średniej
  35. {
  36. if (tab1[i]<nearest)
  37. {
  38. nearest=tab1[i];
  39. nrnearest=i;
  40. }
  41. }
  42. cout<<endl<<tab[nrnearest]<<endl;
  43.  
  44. delete[] tab;
  45. delete[] tab1;
  46. }
  47. }
  48.  
Success #stdin #stdout 0s 3464KB
stdin
3
4 1 2 3 4 
4 4 3 2 1
4 0 3 2 4
stdout
2

3

2