Advertisement
Guest User

lab1

a guest
Oct 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 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.        
  8.     private E data[]; // declared to be an Object since it would be too
  9.                         // complicated with generics
  10.     private int size;
  11.  
  12.     public Array(int size) {
  13.         data = (E[]) new Object[size];
  14.         this.size = size;
  15.     }
  16.  
  17.     public void set(int position, E o) {
  18.         if (position >= 0&&position < size)
  19.             data[position] = o;
  20.         else
  21.             System.out.println("Ne moze da se vmetne element na dadenata pozicija");
  22.     }
  23.  
  24.     public E get(int position) {
  25.         if (position >= 0&&position < size)
  26.             return data[position];
  27.         else
  28.             System.out.println("Ne e validna dadenata pozicija");
  29.         return null;
  30.     }
  31.  
  32.     public int getLength() {
  33.         return size;
  34.     }
  35.    
  36.     public int find(E o) {
  37.         for (int i = 0; i < size; i++){
  38.             if(o.equals(data[i]))
  39.                 return i;
  40.         }
  41.         return -1;
  42.     }
  43.  
  44.     public void insert(int position, E o) {
  45.         // before all we check if position is within range
  46.         if (position >= 0 && position <= size) {
  47.             // first resize the storage array
  48.             E[] newData = (E[]) new Object[size + 1];
  49.             // copy the data prior to the insertion
  50.             for (int i = 0; i < position; i++)
  51.                 newData[i] = data[i];
  52.             // insert the new element
  53.             newData[position] = o;
  54.             // move the data after the insertion
  55.             for (int i = position; i < size; i++)
  56.                 newData[i + 1] = data[i];
  57.             // replace the storage with the new array
  58.             data = newData;
  59.             size = size + 1;
  60.         }
  61.     }
  62.  
  63.     public void delete(int position) {
  64.         // before all we check if position is within range
  65.         if (position >= 0 && position < size) {
  66.             // first resize the storage array
  67.             E[] newData = (E[]) new Object[size - 1];
  68.             // copy the data prior to the delition
  69.             for (int i = 0; i < position; i++)
  70.                 newData[i] = data[i];
  71.             // move the data after the deletion
  72.             for (int i = position + 1; i < size; i++)
  73.                 newData[i - 1] = data[i];
  74.             // replace the storage with the new array
  75.             data = newData;
  76.             size = size - 1;
  77.         }
  78.     }
  79.  
  80.     public void resize(int newSize) {
  81.         // first resize the storage array
  82.         E[] newData = (E[]) new Object[newSize];
  83.         // copy the data
  84.         int copySize = size;
  85.         if (newSize < size)
  86.             copySize = newSize;
  87.         for (int i = 0; i < copySize; i++)
  88.             newData[i] = data[i];
  89.         // replace the storage with the new array
  90.         data = newData;
  91.         size = newSize;
  92.     }
  93.  
  94.         public static int brojDoProsek(Array<Integer> niza){
  95.             int zbir=0,prosek=0,min=100,index=0;
  96.             for(int i=0;i<niza.size;i++){
  97.                 zbir+=niza.get(i);
  98.             }
  99.             prosek=zbir/niza.size;
  100.             for(int i=0;i<niza.size;i++){
  101.                 if(Math.abs(niza.get(i)-prosek)<min){
  102.                     min=Math.abs(niza.get(i)-prosek);
  103.                     index=i;
  104.                 }
  105.                 else if(Math.abs(niza.get(i)-prosek)==min){
  106.                     if(niza.get(index)>niza.get(i)){
  107.                         index=i;
  108.                     }
  109.                 }
  110.                
  111.             }  
  112.             //System.out.println(prosek);
  113.             return niza.get(index);  
  114.         }
  115.        
  116.         public static void main(String[] args) throws IOException{
  117.             BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  118.             String s = stdin.readLine();
  119.             int N = Integer.parseInt(s);
  120.            
  121.             //Vashiot kod tuka...
  122.             Array<Integer> niza=new Array(N);
  123.           for(int i=0;i<N;i++){
  124.               niza.set(i,Integer.parseInt(stdin.readLine()));
  125.           }
  126.           //System.out.println(niza.size);     
  127.             System.out.println(brojDoProsek(niza));    
  128.         }
  129.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement