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.Arrays;
- 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 static int brojDoProsek(Array<Integer> niza){
- double prosek = 0;
- for (int i = 0; i < niza.getLength(); i++) {
- prosek += (double) niza.get(i);
- }
- prosek /= niza.getLength();
- int najblizokBroj = niza.get(0);
- for (int i = 1; i < niza.getLength(); i++) {
- if(Math.abs(niza.get(i) - prosek) < Math.abs(najblizokBroj - prosek)) {
- najblizokBroj = niza.get(i);
- }
- if(Math.abs(niza.get(i) - prosek) == Math.abs(najblizokBroj - prosek)) {
- if(niza.get(i) < najblizokBroj) najblizokBroj = niza.get(i);
- }
- }
- return najblizokBroj;
- }
- 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<Integer>(N);
- for (int i = 0; i < N; i++) {
- niza.set(i, Integer.parseInt(stdin.readLine()));
- }
- System.out.println(brojDoProsek(niza));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement