Mitrezzz

[АПС] Лаб 2 - Средна вредност

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