Advertisement
Chris2o2

[APS] ArrayMeanValue

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