Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- За дадена низа од 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
- **Забелешка:** Да се креира податочна структура низа и истата да се искористи во задачата.
- Влез:
- 5
- 1
- 2
- 3
- 4
- 5
- Излез:
- 3
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- 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> someArray){
- double prosek;
- int zbir=0;
- for(int i = 0; i < someArray.getLength(); i++){
- zbir+= someArray.get(i);
- }
- prosek = (double)zbir/someArray.getLength();
- int x = (int)prosek,broj;
- int final_P=0;
- int currRastojanie,minRastojanie;
- if(prosek - x > 0.5){
- final_P = (int)Math.ceil(prosek);
- }
- else if(prosek - x <= 0.5){
- final_P = (int)Math.floor(prosek);
- }
- minRastojanie = Math.abs(someArray.get(0)-final_P);
- broj = someArray.get(0);
- for(int i = 0; i < someArray.getLength(); i++){
- currRastojanie = Math.abs(someArray.get(i)-final_P);
- if(currRastojanie < minRastojanie){
- minRastojanie = currRastojanie;
- broj = someArray.get(i);
- }
- if(currRastojanie == minRastojanie){
- if(broj > someArray.get(i)){
- minRastojanie = currRastojanie;
- broj = someArray.get(i);
- }
- }
- }
- return broj;
- }
- public static void main(String[] args) throws IOException{
- Scanner myScanner = new Scanner(System.in);
- int n;
- Array<Integer> myArray;
- n = myScanner.nextInt();
- myArray = new Array<Integer>(n);
- for(int i = 0; i < myArray.getLength(); i++){
- myArray.set(i, myScanner.nextInt());
- }
- System.out.println(brojDoProsek(myArray));
- }
- }
Add Comment
Please, Sign In to add comment