Advertisement
sindi29

Round-Robin

Jan 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.69 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.NoSuchElementException;
  5.  
  6. interface Queue<E> {
  7.  
  8.     // Elementi na redicata se objekti od proizvolen tip.
  9.  
  10.     // Metodi za pristap:
  11.  
  12.     public boolean isEmpty ();
  13.         // Vrakja true ako i samo ako redicata e prazena.
  14.  
  15.     public int size ();
  16.         // Ja vrakja dolzinata na redicata.
  17.  
  18.     public E peek ();
  19.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  20.  
  21.     // Metodi za transformacija:
  22.  
  23.     public void clear ();
  24.         // Ja prazni redicata.
  25.  
  26.     public void enqueue (E x);
  27.         // Go dodava x na kraj od redicata.
  28.  
  29.     public E dequeue ();
  30.         // Go otstranuva i vrakja pochetniot element na redicata.
  31.  
  32. }
  33. class SLLNode<E> {
  34.     protected E element;
  35.     protected SLLNode<E> succ;
  36.  
  37.     public SLLNode(E elem, SLLNode<E> succ) {
  38.         this.element = elem;
  39.         this.succ = succ;
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         return element.toString();
  45.     }
  46. }
  47.  
  48.  
  49. class LinkedQueue<E> implements Queue<E> {
  50.  
  51.     // Redicata e pretstavena na sledniot nacin:
  52.     // length go sodrzi brojot na elementi.
  53.     // Elementite se zachuvuvaat vo jazli dod SLL
  54.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  55.     SLLNode<E> front, rear;
  56.     int length;
  57.  
  58.     // Konstruktor ...
  59.  
  60.     public LinkedQueue () {
  61.         clear();
  62.     }
  63.  
  64.     public boolean isEmpty () {
  65.         // Vrakja true ako i samo ako redicata e prazena.
  66.         return (length == 0);
  67.     }
  68.  
  69.     public int size () {
  70.         // Ja vrakja dolzinata na redicata.
  71.         return length;
  72.     }
  73.  
  74.     public E peek () {
  75.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  76.         if (front == null)
  77.             throw new NoSuchElementException();
  78.         return front.element;
  79.     }
  80.  
  81.     public void clear () {
  82.         // Ja prazni redicata.
  83.         front = rear = null;
  84.         length = 0;
  85.     }
  86.  
  87.     public void enqueue (E x) {
  88.         // Go dodava x na kraj od redicata.
  89.         SLLNode<E> latest = new SLLNode<E>(x, null);
  90.         if (rear != null) {
  91.             rear.succ = latest;
  92.             rear = latest;
  93.         } else
  94.             front = rear = latest;
  95.         length++;
  96.     }
  97.  
  98.     public E dequeue () {
  99.         // Go otstranuva i vrakja pochetniot element na redicata.
  100.         if (front != null) {
  101.             E frontmost = front.element;
  102.             front = front.succ;
  103.             if (front == null)  rear = null;
  104.             length--;
  105.             return frontmost;
  106.         } else
  107.             throw new NoSuchElementException();
  108.     }
  109.  
  110. }
  111.  
  112. class ArrayQueue<E> implements Queue<E> {
  113.  
  114.     // Redicata e pretstavena na sledniot nacin:
  115.     // length go sodrzi brojot na elementi.
  116.     // Ako length > 0, togash elementite na redicata se zachuvani vo elems[front...rear-1]
  117.     // Ako rear > front, togash vo  elems[front...maxlength-1] i elems[0...rear-1]
  118.     E[] elems;
  119.     int length, front, rear;
  120.  
  121.     // Konstruktor ...
  122.  
  123.     @SuppressWarnings("unchecked")
  124.     public ArrayQueue (int maxlength) {
  125.         elems = (E[]) new Object[maxlength];
  126.         clear();
  127.     }
  128.  
  129.     public boolean isEmpty () {
  130.         // Vrakja true ako i samo ako redicata e prazena.
  131.         return (length == 0);
  132.     }
  133.  
  134.     public int size () {
  135.         // Ja vrakja dolzinata na redicata.
  136.         return length;
  137.     }
  138.  
  139.     public E peek () {
  140.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  141.         if (length > 0)
  142.             return elems[front];
  143.         else
  144.             throw new NoSuchElementException();
  145.     }
  146.  
  147.     public void clear () {
  148.         // Ja prazni redicata.
  149.         length = 0;
  150.         front = rear = 0;  // arbitrary
  151.     }
  152.  
  153.     public void enqueue (E x) {
  154.         // Go dodava x na kraj od redicata.
  155.         elems[rear++] = x;
  156.         if (rear == elems.length)  rear = 0;
  157.         length++;
  158.     }
  159.  
  160.     public E dequeue () {
  161.         // Go otstranuva i vrakja pochetniot element na redicata.
  162.         if (length > 0) {
  163.             E frontmost = elems[front];
  164.             elems[front++] = null;
  165.             if (front == elems.length)  front = 0;
  166.             length--;
  167.             return frontmost;
  168.         } else
  169.             throw new NoSuchElementException();
  170.     }
  171. }
  172.  
  173. class Procesori
  174. {
  175.     String ime;
  176.     int vremeIzvrshuvanje;
  177.     int vremePristignuvanje;
  178.    
  179.     public Procesori(String ime, int vremeIzvrshuvanje, int vremePristignuvanje) {
  180.         super();
  181.         this.ime = ime;
  182.         this.vremeIzvrshuvanje = vremeIzvrshuvanje;
  183.         this.vremePristignuvanje = vremePristignuvanje;
  184.     }
  185.  
  186.     public String getIme() {
  187.         return ime;
  188.     }
  189.  
  190.     public void setIme(String ime) {
  191.         this.ime = ime;
  192.     }
  193.  
  194.     public int getVremeIzvrshuvanje() {
  195.         return vremeIzvrshuvanje;
  196.     }
  197.  
  198.     public void setVremeIzvrshuvanje(int vremeIzvrshuvanje) {
  199.         this.vremeIzvrshuvanje = vremeIzvrshuvanje;
  200.     }
  201.  
  202.     public int getVremePristignuvanje() {
  203.         return vremePristignuvanje;
  204.     }
  205.  
  206.     public void setVremePristignuvanje(int vremePristignuvanje) {
  207.         this.vremePristignuvanje = vremePristignuvanje;
  208.     }
  209. }
  210.  
  211. public class RoundRobin {
  212.  
  213.     public static void main(String[] args) throws IOException {
  214.         // TODO Auto-generated method stub
  215.        
  216.        
  217.         //treba prvo vo lsita da se sortiraat spored vreme na pristignuvanje i izvrshuvanje, pa posle vo queue
  218.        
  219.        
  220.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  221.         LinkedQueue<Procesori> redica = new LinkedQueue<Procesori>();
  222.         LinkedQueue<Procesori> sortiraj = new LinkedQueue<Procesori>();
  223.        
  224.         String s, ime;
  225.         String[] pom;
  226.         int n, vrI, vrP, t;
  227.        
  228.         s = br.readLine();
  229.         n = Integer.parseInt(s);
  230.        
  231.         for(int i=0; i<n; i++)
  232.         {
  233.             s = br.readLine();
  234.             pom = s.split(" ");
  235.             ime = pom[0];
  236.             vrI = Integer.parseInt(pom[1]);
  237.             vrP = Integer.parseInt(pom[2]);
  238.             Procesori p = new Procesori(ime, vrI, vrP);
  239.             redica.enqueue(p);
  240.            
  241.         }
  242.        
  243.         s = br.readLine();
  244.         t = Integer.parseInt(s);
  245.        
  246.         Procesori max;
  247.         while(!redica.isEmpty())
  248.         {
  249.             max = redica.dequeue();
  250.             for(int i=0; i<redica.size(); i++)
  251.             {
  252.                 Procesori novo = redica.dequeue();
  253.                 if(max.vremePristignuvanje > novo.vremePristignuvanje)
  254.                 {
  255.                     redica.enqueue(max);
  256.                     max = novo;
  257.                 }
  258.                 else
  259.                     if(max.vremePristignuvanje == novo.vremePristignuvanje && max.vremeIzvrshuvanje < novo.vremeIzvrshuvanje)
  260.                 {
  261.                         redica.enqueue(max);
  262.                         max = novo;
  263.                 }
  264.                 else
  265.                     redica.enqueue(novo);
  266.             }
  267.            
  268.             sortiraj.enqueue(max);
  269.         }
  270.        
  271.         while(!sortiraj.isEmpty())
  272.         {
  273.             Procesori novo = sortiraj.dequeue();
  274.             novo.vremeIzvrshuvanje-=t;
  275.             System.out.print(novo.ime + " ");
  276.             if(novo.vremeIzvrshuvanje > 0)
  277.                 sortiraj.enqueue(novo);
  278.         }  
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement