Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Array<E> {
- private E data[]; // declared to be an Object since it would be too
- // complicated with generics
- private int size;
- public Array(int size) {
- data = (E[]) new Object[size];
- this.size = size;
- }
- public void set(int position, E o) {
- if (position >= 0&&position < size)
- data[position] = o;
- else
- System.out.println("Ne moze da se vmetne element na dadenata pozicija");
- }
- public E get(int position) {
- if (position >= 0&&position < size)
- return data[position];
- else
- System.out.println("Ne e validna dadenata pozicija");
- return null;
- }
- public int getLength() {
- return size;
- }
- public int find(E o) {
- for (int i = 0; i < size; i++){
- if(o.equals(data[i]))
- return i;
- }
- return -1;
- }
- public void insert(int position, E o) {
- // before all we check if position is within range
- if (position >= 0 && position <= size) {
- // first resize the storage array
- E[] newData = (E[]) new Object[size + 1];
- // copy the data prior to the insertion
- for (int i = 0; i < position; i++)
- newData[i] = data[i];
- // insert the new element
- newData[position] = o;
- // move the data after the insertion
- for (int i = position; i < size; i++)
- newData[i + 1] = data[i];
- // replace the storage with the new array
- data = newData;
- size = size + 1;
- }
- }
- public void delete(int position) {
- // before all we check if position is within range
- if (position >= 0 && position < size) {
- // first resize the storage array
- E[] newData = (E[]) new Object[size - 1];
- // copy the data prior to the delition
- for (int i = 0; i < position; i++)
- newData[i] = data[i];
- // move the data after the deletion
- for (int i = position + 1; i < size; i++)
- newData[i - 1] = data[i];
- // replace the storage with the new array
- data = newData;
- size = size - 1;
- }
- }
- public void resize(int newSize) {
- // first resize the storage array
- E[] newData = (E[]) new Object[newSize];
- // copy the data
- int copySize = size;
- if (newSize < size)
- copySize = newSize;
- for (int i = 0; i < copySize; i++)
- newData[i] = data[i];
- // replace the storage with the new array
- data = newData;
- size = newSize;
- }
- public static int brojDoProsek(Array<Integer> niza){
- int zbir=0,prosek=0,min=100,index=0;
- for(int i=0;i<niza.size;i++){
- zbir+=niza.get(i);
- }
- prosek=zbir/niza.size;
- for(int i=0;i<niza.size;i++){
- if(Math.abs(niza.get(i)-prosek)<min){
- min=Math.abs(niza.get(i)-prosek);
- index=i;
- }
- else if(Math.abs(niza.get(i)-prosek)==min){
- if(niza.get(index)>niza.get(i)){
- index=i;
- }
- }
- }
- //System.out.println(prosek);
- return niza.get(index);
- }
- public static void main(String[] args) throws IOException{
- BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- //Vashiot kod tuka...
- Array<Integer> niza=new Array(N);
- for(int i=0;i<N;i++){
- niza.set(i,Integer.parseInt(stdin.readLine()));
- }
- //System.out.println(niza.size);
- System.out.println(brojDoProsek(niza));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement