Advertisement
196040

APS zadaci za vezbanje 1 kol Kompanija

Nov 24th, 2020
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class SLLNode {
  5.     protected int id;
  6.     protected int plata;
  7.     protected SLLNode succ;
  8.     public SLLNode(int id,int plata, SLLNode succ) {
  9.         this.id = id;
  10.         this.plata=plata;
  11.         this.succ = succ;
  12.     }
  13. }
  14. class SLL {
  15.     private SLLNode first;
  16.     public SLL() {
  17.         // Construct an empty SLL
  18.         this.first = null;
  19.     }
  20.     public void deleteList() {
  21.         first = null;
  22.     }
  23.     public int length() {
  24.         int ret;
  25.         if (first != null) {
  26.             SLLNode tmp = first;
  27.             ret = 1;
  28.             while (tmp.succ != null) {
  29.                 tmp = tmp.succ;
  30.                 ret++;
  31.             }
  32.             return ret;
  33.         } else
  34.             return 0;
  35.     }
  36.     public void insertFirst(int id, int plata) {
  37.         SLLNode ins = new SLLNode(id,plata, first);
  38.         first = ins;
  39.     }
  40.     public void insertLast(int id,int plata) {
  41.         if (first != null) {
  42.             SLLNode tmp = first;
  43.             while (tmp.succ != null)
  44.                 tmp = tmp.succ;
  45.             SLLNode ins = new SLLNode(id, plata, null);
  46.             tmp.succ = ins;
  47.         } else {
  48.             insertFirst(id,plata);
  49.         }
  50.     }
  51.     public SLLNode getFirst() {
  52.         return first;
  53.     }
  54.     public SLL brisi_pomali_od(int iznos) {
  55.         SLLNode tmp = getFirst();
  56.         SLL novo = new SLL();
  57.         while (tmp != null) {
  58.             if (tmp.plata >= iznos)
  59.                 novo.insertLast(tmp.id, tmp.plata);
  60.             tmp = tmp.succ;
  61.         }
  62.         if(novo.length() ==0) System.out.println("nema");
  63.         return novo;
  64.     }
  65.    
  66.     public SLL sortiraj_opagacki() {
  67.         SLLNode temp = getFirst();//        node temp = head;
  68.         while (temp!=null) {  // Traverse the List
  69.             SLLNode min = temp; //        node min = temp;
  70.             SLLNode r = temp.succ;//   node r = temp.next;
  71.             // Traverse the unsorted sublist
  72.             while (r!=null) {
  73.               if(min.id < r.id) //  if (min.data > r.data)
  74.                     min = r;
  75.  
  76.                 r = r.succ; //r = r.next;
  77.             }
  78.             // Swap Data
  79.             int x = temp.id; //int x = temp.data;
  80.             temp.id = min.id; //temp.data = min.data;
  81.             min.id = x; //min.data = x;
  82.             x = temp.plata;
  83.             temp.plata = min.plata;
  84.             min.plata = x;
  85.            
  86.             temp = temp.succ; //temp = temp.next;
  87.         }
  88.         return this;
  89.     }
  90.     public void pecati (SLL lista)
  91.     {
  92.         SLLNode p=lista.first;
  93.         while(p!=null)
  94.         {
  95.             System.out.println(p.id+" "+p.plata);
  96.             p=p.succ;
  97.         }
  98.     }
  99. }
  100. public class SLLKompanija {
  101.     public static void main(String[] args) throws IOException {
  102.         SLL lista1 = new SLL();
  103.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  104.                 System.in));
  105.         String s = stdin.readLine();
  106.         int N = Integer.parseInt(s);
  107.         for (int i = 0; i < N; i++) {
  108.             s=stdin.readLine();
  109.             String s1=stdin.readLine();
  110.             lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
  111.         }
  112.         s = stdin.readLine();
  113.         lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
  114.         if(lista1!=null)
  115.         {
  116.             lista1=lista1.sortiraj_opagacki();
  117.             lista1.pecati(lista1);
  118.         }
  119.     }
  120. }
  121. /*
  122. Податоците за плати на вработените во една компанија привремено се чуваат во еднострано поврзана листа.
  123. Во секој јазол од листата се чува единствен ID на вработениот и неговата плата. Потребно е да се отстранат
  124. сите вработени со помали плати од даден износ, а остатокот да се прикажат во опаѓачки редослед во однос на ID-то.
  125. Во првиот ред од влезот е даден бројот на вработени, потоа наизменично се дадени ID и плата за секој од вработените
  126.  и во последниот ред е износот во однос на кој ќе се отстрануваат вработените. На излез се печати листа (ID, плата)
  127.   во опаѓачки редослед според ID на секој од вработените.
  128.  
  129. Доколку нема вработени со плата поголема од дадената да се испечати: nema
  130.  
  131. Име на класата: SLLKompanija
  132. */
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement