Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Средна вредност Problem 1 (1 / 1)
- За дадена низа од N (1≤N≤50) природни броеви, да се најде бројот кој е најблиску до нивниот просек. Ако постојат два броја со исто растојание до просекот, да се врати помалиот од нив. На пример за низата 1, 2, 3, 4, 5 просекот е (1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3, што значи дека бројот кој треба да се врати и е најблиску до просекот е 3.
- За низата 1, 2, 3, 4, 5, 6 просекот е 3.5 и двата броја 3 и 4 се на исто растојание од просекот. Точната вредност која треба да се врати е помалиот од нив, а тоа е 3.
- Во низата може да има дупликати.
- Првиот број од влезот е бројот на елементи во низата N, а потоа во секој ред се дадени броевите.
- Име на класата (Java): Array
- **Забелешка:** Да се креира податочна структура низа и истата да се искористи во задачата.
- 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 ){
- double prosek;
- int suma = 0;
- for(int i=0;i<niza.getLength();i++){
- suma += niza.get(i);
- }
- prosek = suma / niza.size;
- double min = 1000000;
- int index1 = 0;
- int index2 = 0;
- double razlika;
- for(int i=0;i<niza.getLength();i++){
- razlika = Math.abs(prosek - niza.get(i));
- if(razlika < min){
- min = razlika;
- index1 = i;
- }
- if(min == razlika){
- index2 = i;
- }
- }
- if(niza.get(index1) < niza.get(index2)){
- return niza.get(index1);
- } else{
- return niza.get(index2);
- }
- }
- 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);
- Array<Integer> niza = new Array<Integer>(N);
- for(int i=0;i<niza.getLength();i++){
- niza.set(i, Integer.parseInt(stdin.readLine()));
- }
- System.out.println(brojDoProsek(niza));
- }
- }
- Sample input
- 5
- 1
- 2
- 3
- 4
- 5
- Sample output
- 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement