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;
- import java.util.Scanner;
- public class Array<E> {
- private E data[];
- private int len;
- public Array(int size) {
- data = (E[]) new Object[size];
- this.len = size;
- }
- public int getLength() {
- return len;
- }
- /*
- public void delete(int position) {
- if (position >= 0 && position < size) {
- for (int i = position - 1; i < size - 1; i++)
- data[i] = data[i + 1];
- size--;
- }
- else throw new IndexOutOfBoundsException();
- }
- 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){
- if (position >= 0 && position < size) {
- E[] newData = (E[])new Object[size + 1];
- System.arraycopy(data, 0, newData, 0, position);
- newData[position] = o;
- System.arraycopy(data, position, newData, position + 1, size - position);
- size = size + 1;
- data = newData;
- }
- else throw new IndexOutOfBoundsException();
- }
- public void resize(int newSize){
- E[] newData = (E[]) new Object[newSize];
- int copySize = size;
- if (newSize < size)
- copySize = newSize;
- for (int i = 0; i < copySize; i++)
- newData[i] = data[i];
- data = newData;
- size = newSize;
- }
- */
- public void set(int position, E obj) throws IndexOutOfBoundsException {
- if (position >= 0 && position < len) {
- data[position] = obj;
- } else {
- System.out.println("Ne moze da se vmetne element na dadenata pozicija");
- throw new IndexOutOfBoundsException();
- }
- }
- public E get(int position) throws IndexOutOfBoundsException {
- if (position >= 0 && position < len) {
- return data[position];
- } else {
- System.out.println("Ne e validna dadenata pozicija");
- throw new IndexOutOfBoundsException();
- }
- }
- public int sum(Array<Integer> array) {
- int sum = 0;
- for (int i = 0; i < array.getLength(); i++) {
- //System.out.println("array.get(i): "+array.get(i)+" len: " + array.getLength() + " i: "+i);
- sum += (int) array.get(i);
- }
- return sum;
- }
- public double average(Array<Integer> array) {
- return array.sum(array) / array.getLength();
- }
- public static int numberToAverage(Array<Integer> array) {
- double average = array.average(array);
- double minDistance = Math.abs(average - array.get(0));
- int index = 0;
- for (int i = 0; i < array.getLength(); i++) {
- //double distance = Math.abs(average) - array.get(i);
- if (minDistance > Math.abs(average - array.get(i))){
- minDistance = Math.abs(average - array.get(i));
- index = i;
- }
- else if (minDistance == Math.abs(average - array.get(i))){
- if (array.get(i) < array.get(index)){
- minDistance = Math.abs(average - array.get(i));
- index = i;
- }
- }
- }
- return array.get(index);
- }
- public static void main(String[] args) throws IOException {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(in.readLine());
- Array<Integer> arr = new Array<Integer>(N);
- for (int i = 0; i < N; i++) {
- arr.set(i, Integer.parseInt(in.readLine()));
- }
- System.out.println(numberToAverage(arr));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement