Advertisement
AnaGocevska

Untitled

Oct 26th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5. import java.lang.Math;
  6.  
  7. public class Array<E> {
  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. }
  95.  
  96. class Niza {
  97.    
  98.     public static int brojDoProsek(Array<Integer> niza)
  99.     {
  100.         //Vashiot kod tuka...
  101.        
  102.         int suma=0;
  103.         double prosek = 0;
  104.         int broj=0;
  105.         int minRast=0;
  106.        
  107.         for(int i=0; i<niza.getLength(); i++)
  108.         {
  109.             suma+=niza.get(i);
  110.         }
  111.        
  112.         prosek = (double)suma/niza.getLength();
  113.        
  114.         double decimal = prosek - Math.floor(prosek);
  115.        
  116.         if(decimal < 0.5)
  117.         {
  118.             broj = (int)Math.floor(prosek);
  119.         }
  120.         else if(decimal > 0.5)
  121.         {
  122.             broj = (int)Math.ceil(prosek);
  123.         }
  124.         else
  125.         {
  126.             broj = (int)prosek;
  127.         }
  128.        
  129.         minRast = Math.abs(niza.get(0)-broj);
  130.         int number = niza.get(0);
  131.        
  132.         for(int i=0; i<niza.getLength(); i++)
  133.         {
  134.             if(Math.abs(niza.get(i)-broj) < minRast)
  135.             {
  136.                 minRast = Math.abs(niza.get(i)-broj);
  137.                 number = niza.get(i);
  138.             }
  139.             if(Math.abs(niza.get(i) - broj) == minRast)
  140.             {
  141.                 if(niza.get(i) < number)
  142.                 {
  143.                     number = niza.get(i);
  144.                 }
  145.             }
  146.            
  147.         }
  148.         return (int)number;
  149.     }
  150.    
  151.     public static void main(String[] args)
  152.     {
  153.         Scanner s = new Scanner(System.in);
  154.        
  155.         int n = s.nextInt();
  156.        
  157.         Array<Integer> niza = new Array<Integer>(n);
  158.        
  159.         for(int i=0; i<n; i++)
  160.         {
  161.             niza.set(i, s.nextInt());
  162.         }
  163.        
  164.        System.out.print(brojDoProsek(niza));
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement