Guest User

Untitled

a guest
Aug 23rd, 2019
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. class Array<E>
  7. {
  8.  
  9. }
  10. public class SpojListi {
  11. public static int presmetaj(int[] niza)
  12. {
  13. int sum=0;
  14. int average=0;
  15. for(int i = 0; i < niza.length; i++)
  16. {
  17. sum += niza[i];
  18. }
  19. average = sum / niza.length;
  20. int kandidat = niza[0];
  21. int razlika = Math.abs(average - niza[0]);
  22.  
  23. for(int i = 1; i < niza.length; i++)
  24. {
  25. if(Math.abs(average-niza[i]) < razlika)
  26. {
  27. razlika = Math.abs(average-niza[i]);
  28. kandidat = niza[i];
  29. }
  30.  
  31. else if(Math.abs(niza[i]-average) == 0)
  32. {
  33. kandidat = niza[i];
  34. }
  35. }
  36. return kandidat;
  37. }
  38.  
  39. public static void main(String[] args) throws IOException
  40. {
  41. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  42.  
  43. //vnes za golemina na nizata
  44. String s = input.readLine();
  45. int N = Integer.parseInt(s);
  46.  
  47. if(!(1<= N && N <=50))
  48. {
  49. return;
  50. }
  51. // alociranje na memorija za niza so N elementi
  52. int[] niza = new int [N];
  53.  
  54. for(int i = 0; i < N; i++)
  55. {
  56. niza[i] = Integer.parseInt(input.readLine());
  57. }
  58. System.out.println(presmetaj(niza));
  59. }
  60. }
RAW Paste Data