Kame3

Компанија

Nov 23rd, 2020 (edited)
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.60 KB | None | 0 0
  1. Компанија Problem 10 (4 / 10)
  2.  
  3. Податоците за плати на вработените во една компанија привремено се чуваат во еднострано поврзана листа. Во секој јазол од листата се чува единствен ID на вработениот и неговата плата. Потребно е да се отстранат сите вработени со помали плати од даден износ, а остатокот да се прикажат во опаѓачки редослед во однос на ID-то. Во првиот ред од влезот е даден бројот на вработени, потоа наизменично се дадени ID и плата за секој од вработените и во последниот ред е износот во однос на кој ќе се отстрануваат вработените. На излез се печати листа (ID, плата) во опаѓачки редослед според ID на секој од вработените.
  4.  
  5. Доколку нема вработени со плата поголема од дадената да се испечати: nema
  6.  
  7. Име на класата: SLLKompanija
  8.  
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.util.Iterator;
  14. import java.util.NoSuchElementException;
  15.  
  16. class SLL<E> {
  17.     private SLLNode<E> first;
  18.  
  19.     public SLL() {
  20.         // Construct an empty SLL
  21.         this.first = null;
  22.     }
  23.  
  24.     public void deleteList() {
  25.         first = null;
  26.     }
  27.  
  28.     public int length() {
  29.         int ret;
  30.         if (first != null) {
  31.             SLLNode<E> tmp = first;
  32.             ret = 1;
  33.             while (tmp.succ != null) {
  34.                 tmp = tmp.succ;
  35.                 ret++;
  36.             }
  37.             return ret;
  38.         } else
  39.             return 0;
  40.  
  41.     }
  42.  
  43.     @Override
  44.     public String toString() {
  45.         String ret = new String();
  46.         if (first != null) {
  47.             SLLNode<E> tmp = first;
  48.             ret += tmp + "->";
  49.             while (tmp.succ != null) {
  50.                 tmp = tmp.succ;
  51.                 ret += tmp + "->";
  52.             }
  53.         } else
  54.             ret = "Prazna lista!!!";
  55.         return ret;
  56.     }
  57.  
  58.     public void insertFirst(E o) {
  59.         SLLNode<E> ins = new SLLNode<E>(o, first);
  60.         first = ins;
  61.     }
  62.  
  63.     public void insertAfter(E o, SLLNode<E> node) {
  64.         if (node != null) {
  65.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  66.             node.succ = ins;
  67.         } else {
  68.             System.out.println("Dadenot jazol e null");
  69.         }
  70.     }
  71.  
  72.     public void insertBefore(E o, SLLNode<E> before) {
  73.  
  74.         if (first != null) {
  75.             SLLNode<E> tmp = first;
  76.             if(first==before){
  77.                 this.insertFirst(o);
  78.                 return;
  79.             }
  80.             //ako first!=before
  81.             while (tmp.succ != before)
  82.                 tmp = tmp.succ;
  83.             if (tmp.succ == before) {
  84.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  85.                 tmp.succ = ins;
  86.             } else {
  87.                 System.out.println("Elementot ne postoi vo listata");
  88.             }
  89.         } else {
  90.             System.out.println("Listata e prazna");
  91.         }
  92.     }
  93.  
  94.     public void insertLast(E o) {
  95.         if (first != null) {
  96.             SLLNode<E> tmp = first;
  97.             while (tmp.succ != null)
  98.                 tmp = tmp.succ;
  99.             SLLNode<E> ins = new SLLNode<E>(o, null);
  100.             tmp.succ = ins;
  101.         } else {
  102.             insertFirst(o);
  103.         }
  104.     }
  105.  
  106.     public E deleteFirst() {
  107.         if (first != null) {
  108.             SLLNode<E> tmp = first;
  109.             first = first.succ;
  110.             return tmp.element;
  111.         } else {
  112.             System.out.println("Listata e prazna");
  113.             return null;
  114.         }
  115.     }
  116.  
  117.     public E delete(SLLNode<E> node) {
  118.         if (first != null) {
  119.             SLLNode<E> tmp = first;
  120.             if(first ==node){
  121.                 return this.deleteFirst();
  122.             }
  123.             while (tmp.succ != node && tmp.succ.succ != null)
  124.                 tmp = tmp.succ;
  125.             if (tmp.succ == node) {
  126.                 tmp.succ = tmp.succ.succ;
  127.                 return node.element;
  128.             } else {
  129.                 System.out.println("Elementot ne postoi vo listata");
  130.                 return null;
  131.             }
  132.         } else {
  133.             System.out.println("Listata e prazna");
  134.             return null;
  135.         }
  136.  
  137.     }
  138.  
  139.     public SLLNode<E> getFirst() {
  140.         return first;
  141.     }
  142.  
  143.     public SLLNode<E> find(E o) {
  144.         if (first != null) {
  145.             SLLNode<E> tmp = first;
  146.             while (tmp.element != o && tmp.succ != null)
  147.                 tmp = tmp.succ;
  148.             if (tmp.element == o) {
  149.                 return tmp;
  150.             } else {
  151.                 System.out.println("Elementot ne postoi vo listata");
  152.             }
  153.         } else {
  154.             System.out.println("Listata e prazna");
  155.         }
  156.         return first;
  157.     }
  158.  
  159.     public Iterator<E> iterator () {
  160.         // Return an iterator that visits all elements of this list, in left-to-right order.
  161.         return new LRIterator<E>();
  162.     }
  163.  
  164.     // //////////Inner class ////////////
  165.  
  166.     private class LRIterator<E> implements Iterator<E> {
  167.  
  168.         private SLLNode<E> place, curr;
  169.  
  170.         private LRIterator() {
  171.             place = (SLLNode<E>) first;
  172.             curr = null;
  173.         }
  174.  
  175.         public boolean hasNext() {
  176.             return (place != null);
  177.         }
  178.  
  179.         public E next() {
  180.             if (place == null)
  181.                 throw new NoSuchElementException();
  182.             E nextElem = place.element;
  183.             curr = place;
  184.             place = place.succ;
  185.             return nextElem;
  186.         }
  187.  
  188.         public void remove() {
  189.             //Not implemented
  190.         }
  191.     }
  192.  
  193.     public void mirror(){
  194.         if (first != null) {
  195.             //m=nextsucc, p=tmp,q=next
  196.             SLLNode<E> tmp = first;
  197.             SLLNode<E> newsucc = null;
  198.             SLLNode<E> next;
  199.  
  200.             while(tmp != null){
  201.                 next = tmp.succ;
  202.                 tmp.succ = newsucc;
  203.                 newsucc = tmp;
  204.                 tmp = next;
  205.             }
  206.             first = newsucc;
  207.         }
  208.  
  209.     }
  210.  
  211.     public void merge (SLL<E> in){
  212.         if (first != null) {
  213.             SLLNode<E> tmp = first;
  214.             while(tmp.succ != null)
  215.                 tmp = tmp.succ;
  216.             tmp.succ = in.getFirst();
  217.         }
  218.         else{
  219.             first = in.getFirst();
  220.         }
  221.     }
  222. }
  223. class SLLNode<E> {
  224.     protected E element;
  225.     protected SLLNode<E> succ;
  226.  
  227.     public SLLNode(E elem, SLLNode<E> succ) {
  228.         this.element = elem;
  229.         this.succ = succ;
  230.     }
  231.  
  232.     @Override
  233.     public String toString() {
  234.         return element.toString();
  235.     }
  236. }
  237. class Vraboten {
  238.     int id;
  239.     int plata;
  240.     public Vraboten(){}
  241.     public Vraboten(int id, int plata) {
  242.         this.id = id;
  243.         this.plata = plata;
  244.     }
  245.     public void setId(int id) {
  246.         this.id = id;
  247.     }
  248.     public void setPlata(int plata) {
  249.         this.plata = plata;
  250.     }
  251.     public int getId() {
  252.         return this.id;
  253.     }
  254.     public int getPlata() {
  255.         return this.plata;
  256.     }
  257. }
  258.  
  259. public class FuckingFuck {
  260.     public static SLL<Vraboten> brisi_pomali_od_iznos(SLL<Vraboten> lista, int x) {
  261.         SLLNode<Vraboten> d1 = lista.getFirst();
  262.  
  263.         while (d1 != null) {
  264.             if (d1.element.plata < x) {
  265.                 lista.delete(d1);
  266.                 d1 = d1.succ;
  267.                 continue;
  268.             } else {
  269.                 d1 = d1.succ;
  270.             }
  271.         }
  272.  
  273.         return lista;
  274.     }
  275.     public static void sortiraj_opagjacki(SLL<Vraboten> lista) {
  276.  
  277.         for (int i=0; i< lista.length(); i++) {
  278.             SLLNode<Vraboten> d1 = lista.getFirst();
  279.             while (d1.succ != null) {
  280.                 if (d1.element.id < d1.succ.element.id) {
  281.                     int x = d1.element.id;
  282.                     d1.element.id =  d1.succ.element.id;
  283.                     d1.succ.element.id = x;
  284.  
  285.                     int y = d1.element.plata;
  286.                     d1.element.plata =  d1.succ.element.plata;
  287.                     d1.succ.element.plata = y;
  288.                 }
  289.                 d1 = d1.succ;
  290.             }
  291.         }
  292.  
  293.         SLLNode<Vraboten> d1 = lista.getFirst();
  294.         while (d1 != null) {
  295.             System.out.println(d1.element.id + " " + d1.element.plata);
  296.             d1 = d1.succ;
  297.         }
  298.     }
  299.     public static void main (String[] args) throws IOException {
  300.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  301.  
  302.         SLL<Vraboten> vraboteni = new SLL<>();
  303.         int n = Integer.parseInt(br.readLine());
  304.         for (int i=0; i<n; i++) {
  305.             int id = Integer.parseInt(br.readLine());
  306.             int plata = Integer.parseInt(br.readLine());
  307.             Vraboten v = new Vraboten(id,plata);
  308.             vraboteni.insertLast(v);
  309.         }
  310.  
  311.         int iznos = Integer.parseInt(br.readLine());
  312.  
  313.         SLL<Vraboten> result = brisi_pomali_od_iznos(vraboteni,iznos);
  314.         sortiraj_opagjacki(result);
  315.     }
  316. }
  317.  
  318.  
  319.  
  320.  
  321.  
  322.    
  323. Input:
  324. 10
  325. 10
  326. 19000
  327. 18
  328. 25000
  329. 17
  330. 23000
  331. 19
  332. 25000
  333. 14
  334. 34000
  335. 55
  336. 23400
  337. 66
  338. 23670
  339. 34
  340. 12670
  341. 46
  342. 21300
  343. 33
  344. 34550
  345. 6000
  346.  
  347.    
  348. Output:
  349. 66 23670
  350. 55 23400
  351. 46 21300
  352. 34 12670
  353. 33 34550
  354. 19 25000
  355. 18 25000
  356. 17 23000
  357. 14 34000
  358. 10 19000
  359.  
Add Comment
Please, Sign In to add comment