Advertisement
ekrajchevska

[АПС] Средна вредност

Apr 10th, 2017
2,755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. /* 1. Средна вредност - Java Задача 1 (0 / 0)
  2. За дадена низа од 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, а потоа во секој ред се дадени броевите. Име на класата: Array
  3. Вашето решение:
  4. Програмски јазик: Java
  5.  
  6. Sample input:
  7. 14
  8. 74
  9. 93
  10. 98
  11. 6
  12. 11
  13. 46
  14. 16
  15. 93
  16. 49
  17. 54
  18. 57
  19. 32
  20. 72
  21. 91
  22. Sample output:
  23. 57
  24.  
  25. Sample input:
  26. 4
  27. 2
  28. 3
  29. 7
  30. 8
  31. Sample output:
  32. 3 */
  33.  
  34. package sll.srednavrednost;
  35.  
  36. import java.io.BufferedReader;
  37. import java.io.IOException;
  38. import java.io.InputStreamReader;
  39. import java.util.Iterator;
  40. import java.util.NoSuchElementException;
  41.  
  42. class SLLNode<E> {
  43.     protected E element;
  44.     protected SLLNode<E> succ;
  45.  
  46.     public SLLNode(E elem, SLLNode<E> succ) {
  47.         this.element = elem;
  48.         this.succ = succ;
  49.     }
  50.  
  51.     @Override
  52.     public String toString() {
  53.         return element.toString();
  54.     }
  55. }
  56. class SLL<E> {
  57.     private SLLNode<E> first;
  58.  
  59.     public SLL() {
  60.         // Construct an empty SLL
  61.         this.first = null;
  62.     }
  63.  
  64.     public void deleteList() {
  65.         first = null;
  66.     }
  67.  
  68.     public int length() {
  69.         int ret;
  70.         if (first != null) {
  71.             SLLNode<E> tmp = first;
  72.             ret = 1;
  73.             while (tmp.succ != null) {
  74.                 tmp = tmp.succ;
  75.                 ret++;
  76.             }
  77.             return ret;
  78.         } else
  79.             return 0;
  80.  
  81.     }
  82.  
  83.     @Override
  84.     public String toString() {
  85.         String ret = new String();
  86.         if (first != null) {
  87.             SLLNode<E> tmp = first;
  88.             ret += tmp + "->";
  89.             while (tmp.succ != null) {
  90.                 tmp = tmp.succ;
  91.                 ret += tmp + "->";
  92.             }
  93.         } else
  94.             ret = "Prazna lista!!!";
  95.         return ret;
  96.     }
  97.  
  98.     public void insertFirst(E o) {
  99.         SLLNode<E> ins = new SLLNode<E>(o, first);
  100.         first = ins;
  101.     }
  102.  
  103.     public void insertAfter(E o, SLLNode<E> node) {
  104.         if (node != null) {
  105.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  106.             node.succ = ins;
  107.         } else {
  108.             System.out.println("Dadenot jazol e null");
  109.         }
  110.     }
  111.  
  112.     public void insertBefore(E o, SLLNode<E> before) {
  113.        
  114.         if (first != null) {
  115.             SLLNode<E> tmp = first;
  116.             if(first==before){
  117.                 this.insertFirst(o);
  118.                 return;
  119.             }
  120.             //ako first!=before
  121.             while (tmp.succ != before)
  122.                 tmp = tmp.succ;
  123.             if (tmp.succ == before) {
  124.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  125.                 tmp.succ = ins;
  126.             } else {
  127.                 System.out.println("Elementot ne postoi vo listata");
  128.             }
  129.         } else {
  130.             System.out.println("Listata e prazna");
  131.         }
  132.     }
  133.  
  134.     public void insertLast(E o) {
  135.         if (first != null) {
  136.             SLLNode<E> tmp = first;
  137.             while (tmp.succ != null)
  138.                 tmp = tmp.succ;
  139.             SLLNode<E> ins = new SLLNode<E>(o, null);
  140.             tmp.succ = ins;
  141.         } else {
  142.             insertFirst(o);
  143.         }
  144.     }
  145.  
  146.     public E deleteFirst() {
  147.         if (first != null) {
  148.             SLLNode<E> tmp = first;
  149.             first = first.succ;
  150.             return tmp.element;
  151.         } else {
  152.             System.out.println("Listata e prazna");
  153.             return null;
  154.         }
  155.     }
  156.  
  157.     public E delete(SLLNode<E> node) {
  158.         if (first != null) {
  159.             SLLNode<E> tmp = first;
  160.             if(first ==node){
  161.                 return this.deleteFirst();
  162.             }
  163.             while (tmp.succ != node && tmp.succ.succ != null)
  164.                 tmp = tmp.succ;
  165.             if (tmp.succ == node) {
  166.                 tmp.succ = tmp.succ.succ;
  167.                 return node.element;
  168.             } else {
  169.                 System.out.println("Elementot ne postoi vo listata");
  170.                 return null;
  171.             }
  172.         } else {
  173.             System.out.println("Listata e prazna");
  174.             return null;
  175.         }
  176.  
  177.     }
  178.  
  179.     public SLLNode<E> getFirst() {
  180.         return first;
  181.     }
  182.    
  183.     public SLLNode<E> find(E o) {
  184.         if (first != null) {
  185.             SLLNode<E> tmp = first;
  186.             while (tmp.element != o && tmp.succ != null)
  187.                 tmp = tmp.succ;
  188.             if (tmp.element == o) {
  189.                 return tmp;
  190.             } else {
  191.                 System.out.println("Elementot ne postoi vo listata");
  192.             }
  193.         } else {
  194.             System.out.println("Listata e prazna");
  195.         }
  196.         return first;
  197.     }
  198.    
  199.     public Iterator<E> iterator () {
  200.     // Return an iterator that visits all elements of this list, in left-to-right order.
  201.         return new LRIterator<E>();
  202.     }
  203.  
  204.     // //////////Inner class ////////////
  205.  
  206.     private class LRIterator<E> implements Iterator<E> {
  207.  
  208.         private SLLNode<E> place, curr;
  209.  
  210.         private LRIterator() {
  211.             place = (SLLNode<E>) first;
  212.             curr = null;
  213.         }
  214.  
  215.         public boolean hasNext() {
  216.             return (place != null);
  217.         }
  218.  
  219.         public E next() {
  220.             if (place == null)
  221.                 throw new NoSuchElementException();
  222.             E nextElem = place.element;
  223.             curr = place;
  224.             place = place.succ;
  225.             return nextElem;
  226.         }
  227.  
  228.         public void remove() {
  229.             //Not implemented
  230.         }
  231.     }
  232.    
  233.     public void mirror(){
  234.         if (first != null) {
  235.             //m=nextsucc, p=tmp,q=next
  236.             SLLNode<E> tmp = first;
  237.             SLLNode<E> newsucc = null;
  238.             SLLNode<E> next;
  239.            
  240.             while(tmp != null){
  241.                 next = tmp.succ;
  242.                 tmp.succ = newsucc;
  243.                 newsucc = tmp;
  244.                 tmp = next;
  245.             }
  246.             first = newsucc;
  247.         }
  248.        
  249.     }
  250.    
  251.     public void merge (SLL<E> in){
  252.         if (first != null) {
  253.             SLLNode<E> tmp = first;
  254.             while(tmp.succ != null)
  255.                 tmp = tmp.succ;
  256.             tmp.succ = in.getFirst();
  257.         }
  258.         else{
  259.             first = in.getFirst();
  260.         }
  261.     }
  262. }
  263.  
  264. public class SLLSrednaVrednost {
  265.  
  266.     public static int najbliskuDoProsek(SLL<Integer> lista)
  267.     {
  268.         SLLNode<Integer> curr = lista.getFirst();
  269.         int zbir=0, br=0, rez=0, prosek,razlika=100;
  270.         while(curr!=null)
  271.         {
  272.             zbir+=curr.element;
  273.             br++;
  274.             curr=curr.succ;
  275.         }
  276.         prosek = zbir/br;
  277.        
  278.         curr = lista.getFirst();
  279.         while(curr!=null)
  280.         {
  281.             if(curr.element== prosek) return prosek;
  282.             else if(Math.abs(prosek-curr.element)<razlika)
  283.             {
  284.                 razlika = Math.abs(prosek-curr.element);
  285.                 rez = curr.element;
  286.             }
  287.             else if(Math.abs(prosek-curr.element)==razlika)
  288.             {
  289.                 if(curr.element<rez) rez=curr.element;
  290.             }
  291.            
  292.             curr=curr.succ;
  293.         }
  294.         return rez;
  295.        
  296.     }
  297.    
  298.     public static void main(String[] args) throws IOException {
  299.         // TODO code application logic here
  300.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  301.         String s = br.readLine();
  302.         int N = Integer.parseInt(s);
  303.        
  304.         SLL<Integer> lista = new SLL();
  305.         for(int i=0;i<N;i++)
  306.         {
  307.             s = br.readLine();
  308.             lista.insertLast(Integer.parseInt(s));
  309.         }
  310.        
  311.         System.out.println(najbliskuDoProsek(lista));
  312.     }
  313.    
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement