kacci97

Средна вредност

Oct 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class Array<E> {
  6. private E niza[];
  7. private int n;
  8.  
  9.  
  10. public Array(int n) {
  11. niza = (E[]) new Object[n];
  12. this.n=n;
  13. }
  14.  
  15. public void set(int position, E ob)
  16. {
  17. for(int i=0;i<n;i++)
  18. {
  19. if(position>=0&&position<n)
  20. {
  21. niza[position] = ob;
  22. }
  23. }
  24. }
  25.  
  26. public E get(int position)
  27. {
  28. if(position>=0 && position<n)
  29. return niza[position];
  30. else
  31. System.out.println("Nevalidna pozicija");
  32. return null;
  33. }
  34.  
  35. public int find(E ob)
  36. {
  37. for(int i=0;i<n;i++)
  38. {
  39. if(niza[i].equals(ob)==true)
  40. {
  41. return i;
  42. }
  43. }
  44. return -1;
  45. }
  46.  
  47. public int getLength()
  48. {
  49. return n;
  50. }
  51.  
  52.  
  53. public static int brojDoProsek(Array<Integer> niza){
  54.  
  55. int suma=0,prosek=0;
  56.  
  57. for(int i=0;i<niza.getLength();i++)
  58. {
  59. suma = suma + niza.get(i);
  60. }
  61. prosek = suma/niza.getLength();
  62.  
  63. int min = Math.abs(niza.get(0)-prosek);
  64. int index = 0;
  65.  
  66. for(int i=1;i<niza.getLength();i++)
  67. {
  68. if(Math.abs(niza.get(i)-prosek) < min)
  69. {
  70. min = Math.abs(niza.get(i) - prosek);
  71. index = i;
  72. }
  73. if(Math.abs(niza.get(i)-prosek) == min)
  74. {
  75. if( niza.get(i) < niza.get(index) )
  76. {
  77. min = Math.abs(niza.get(i)-prosek);
  78. index = i;
  79. }
  80. }
  81. }
  82. if(min==0) return prosek;
  83. else
  84. return niza.get(index);
  85.  
  86. }
  87.  
  88. public static void main(String[] args) throws IOException{
  89. BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  90. String s = stdin.readLine();
  91. int N = Integer.parseInt(s);
  92.  
  93. //Vashiot kod tuka...
  94. Array<Integer> niza = new Array<Integer>(N);
  95. for(int i=0;i<N;i++)
  96. {
  97. s=stdin.readLine();
  98. niza.set(i,Integer.parseInt(s));
  99. }
  100.  
  101.  
  102. System.out.println(brojDoProsek(niza));
  103. }}
Add Comment
Please, Sign In to add comment