Advertisement
Kame3

Средна вредност lab2.1

Feb 17th, 2021
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. Средна вредност Problem 1 (1 / 1)
  2.  
  3. За дадена низа од N (1≤N≤50) природни броеви, да се најде бројот кој е најблиску до нивниот просек. Ако постојат два броја со исто растојание до просекот, да се врати помалиот од нив. На пример за низата 1, 2, 3, 4, 5 просекот е (1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3, што значи дека бројот кој треба да се врати и е најблиску до просекот е 3.
  4.  
  5. За низата 1, 2, 3, 4, 5, 6 просекот е 3.5 и двата броја 3 и 4 се на исто растојание од просекот. Точната вредност која треба да се врати е помалиот од нив, а тоа е 3.
  6.  
  7. Во низата може да има дупликати.
  8.  
  9. Првиот број од влезот е бројот на елементи во низата N, а потоа во секој ред се дадени броевите.
  10.  
  11. Име на класата (Java): Array
  12. **Забелешка:** Да се креира податочна структура низа и истата да се искористи во задачата.
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17.  
  18. public class Array<E> {
  19.    
  20.         private E data[]; // declared to be an Object since it would be too
  21.                         // complicated with generics
  22.     private int size;
  23.  
  24.     public Array(int size) {
  25.         data = (E[]) new Object[size];
  26.         this.size = size;
  27.     }
  28.  
  29.     public void set(int position, E o) {
  30.         if (position >= 0&&position < size)
  31.             data[position] = o;
  32.         else
  33.             System.out.println("Ne moze da se vmetne element na dadenata pozicija");
  34.     }
  35.  
  36.     public E get(int position) {
  37.         if (position >= 0 && position < size)
  38.             return data[position];
  39.         else
  40.             System.out.println("Ne e validna dadenata pozicija");
  41.         return null;
  42.     }
  43.  
  44.     public int getLength() {
  45.         return size;
  46.     }
  47.    
  48.     public int find(E o) {
  49.         for (int i = 0; i < size; i++){
  50.             if(o.equals(data[i]))
  51.                 return i;
  52.         }
  53.         return -1;
  54.     }
  55.  
  56.     public void insert(int position, E o) {
  57.         // before all we check if position is within range
  58.         if (position >= 0 && position <= size) {
  59.             // first resize the storage array
  60.             E[] newData = (E[]) new Object[size + 1];
  61.             // copy the data prior to the insertion
  62.             for (int i = 0; i < position; i++)
  63.                 newData[i] = data[i];
  64.             // insert the new element
  65.             newData[position] = o;
  66.             // move the data after the insertion
  67.             for (int i = position; i < size; i++)
  68.                 newData[i + 1] = data[i];
  69.             // replace the storage with the new array
  70.             data = newData;
  71.             size = size + 1;
  72.         }
  73.     }
  74.  
  75.     public void delete(int position) {
  76.         // before all we check if position is within range
  77.         if (position >= 0 && position < size) {
  78.             // first resize the storage array
  79.             E[] newData = (E[]) new Object[size - 1];
  80.             // copy the data prior to the delition
  81.             for (int i = 0; i < position; i++)
  82.                 newData[i] = data[i];
  83.             // move the data after the deletion
  84.             for (int i = position + 1; i < size; i++)
  85.                 newData[i - 1] = data[i];
  86.             // replace the storage with the new array
  87.             data = newData;
  88.             size = size - 1;
  89.         }
  90.     }
  91.  
  92.     public void resize(int newSize) {
  93.         // first resize the storage array
  94.         E[] newData = (E[]) new Object[newSize];
  95.         // copy the data
  96.         int copySize = size;
  97.         if (newSize < size)
  98.             copySize = newSize;
  99.         for (int i = 0; i < copySize; i++)
  100.             newData[i] = data[i];
  101.         // replace the storage with the new array
  102.         data = newData;
  103.         size = newSize;
  104.     }
  105.    
  106.     public static int brojDoProsek(Array<Integer> niza ){
  107.         double prosek;
  108.         int suma = 0;
  109.         for(int i=0;i<niza.getLength();i++){
  110.             suma += niza.get(i);
  111.         }
  112.         prosek = suma / niza.size;
  113.        
  114.         double min = 1000000;
  115.         int index1 = 0;
  116.         int index2 = 0;
  117.         double razlika;
  118.         for(int i=0;i<niza.getLength();i++){
  119.             razlika = Math.abs(prosek - niza.get(i));
  120.             if(razlika < min){
  121.                 min = razlika;
  122.                 index1 = i;
  123.             }
  124.             if(min == razlika){
  125.                 index2 = i;
  126.             }
  127.         }
  128.        
  129.         if(niza.get(index1) < niza.get(index2)){
  130.             return niza.get(index1);
  131.         } else{
  132.             return niza.get(index2);
  133.         }
  134.        
  135.     }
  136.    
  137.     public static void main(String[] args) throws IOException{
  138.         BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  139.         String s = stdin.readLine();
  140.         int N = Integer.parseInt(s);
  141.         Array<Integer> niza = new Array<Integer>(N);
  142.         for(int i=0;i<niza.getLength();i++){
  143.             niza.set(i, Integer.parseInt(stdin.readLine()));
  144.         }
  145.        
  146.         System.out.println(brojDoProsek(niza));    
  147.     }
  148.    
  149.    
  150.  
  151. }
  152.  
  153.  
  154.  
  155. Sample input
  156.  
  157. 5
  158. 1
  159. 2
  160. 3
  161. 4
  162. 5
  163.  
  164. Sample output
  165.  
  166. 3
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement