Crazy

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

Oct 21st, 2017
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Array<E> {
  6.  
  7.     private E niza[];
  8.     private int size;
  9.  
  10.     public void setNiza(E[] niza)
  11.     {
  12.         this.niza=niza;
  13.     }
  14.  
  15.     public int getSize(){
  16.         return size;
  17.     }
  18.  
  19.     public void setSize(int size)
  20.     {
  21.         this.size=size;
  22.     }
  23.  
  24.     public E[] getNiza()
  25.     {
  26.         return niza;
  27.     }
  28.  
  29.     public Array(int size)
  30.     {
  31.         this.size=size;
  32.     }
  33.  
  34.     public E get(int position){return niza[position];}
  35.  
  36.  
  37.     public static int brojDoProsek(Array<Integer> niza ){
  38.         int suma = 0;
  39.         for(int i = 0; i < niza.getSize(); i++){
  40.             suma += niza.get(i);
  41.         }
  42.         double prosek = (double)suma/niza.getSize();
  43.         int element = niza.get(0);
  44.         double min = suma;
  45.         for(int i = 1; i < niza.getSize(); i++) {
  46.             if(min > Math.abs( niza.get(i) - prosek)){
  47.                 min = Math.abs( niza.get(i) - prosek);
  48.                 element = niza.get(i);
  49.             }
  50.             else if( min == Math.abs( niza.get(i) - prosek) && niza.get(i) < element){
  51.                 element = niza.get(i);
  52.             }
  53.         }
  54.         return element;
  55.     }
  56.  
  57.  
  58.  
  59.    
  60.  
  61.     public static void main(String[] args) throws IOException{
  62.         BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  63.         String s = stdin.readLine();
  64.         int N = Integer.parseInt(s);
  65.  
  66.         //Vashiot kod tuka...
  67.         Array<Integer> niza = new Array<>(N);
  68.         Integer array[] = new Integer[N];
  69.  
  70.         for (int i=0;i<N;i++)
  71.         {
  72.             s=stdin.readLine();
  73.             array[i]=Integer.parseInt(s);
  74.  
  75.             niza.setNiza(array);
  76.         }
  77.  
  78.  
  79.         System.out.println(brojDoProsek(niza));
  80.     }
  81.  
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment