Advertisement
1cutcut1

prva

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