Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2. import java.util.Scanner;
  3.  
  4. class SLLNode<E> {
  5.     protected E element;
  6.     protected SLLNode<E> succ;
  7.  
  8.     public SLLNode(E elem, SLLNode<E> succ) {
  9.         this.element = elem;
  10.         this.succ = succ;
  11.     }
  12.  
  13.     @Override
  14.     public String toString() {
  15.         return element.toString();
  16.     }
  17. }
  18.  
  19.  
  20. interface Queue<E> {
  21.  
  22.     // Elementi na redicata se objekti od proizvolen tip.
  23.  
  24.     // Metodi za pristap:
  25.  
  26.     public boolean isEmpty ();
  27.         // Vrakja true ako i samo ako redicata e prazena.
  28.  
  29.     public int size ();
  30.         // Ja vrakja dolzinata na redicata.
  31.  
  32.     public E peek ();
  33.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  34.  
  35.     // Metodi za transformacija:
  36.  
  37.     public void clear ();
  38.         // Ja prazni redicata.
  39.  
  40.     public void enqueue (E x);
  41.         // Go dodava x na kraj od redicata.
  42.  
  43.     public E dequeue ();
  44.         // Go otstranuva i vrakja pochetniot element na redicata.
  45. }
  46.  
  47. class LinkedQueue<E> implements Queue<E> {
  48.  
  49.     // Redicata e pretstavena na sledniot nacin:
  50.     // length go sodrzi brojot na elementi.
  51.     // Elementite se zachuvuvaat vo jazli dod SLL
  52.     // front i rear se linkovi do prviot i posledniot jazel soodvetno.
  53.     SLLNode<E> front, rear;
  54.     int length;
  55.  
  56.     // Konstruktor ...
  57.  
  58.     public LinkedQueue () {
  59.         clear();
  60.     }
  61.  
  62.     public boolean isEmpty () {
  63.         // Vrakja true ako i samo ako redicata e prazena.
  64.         return (length == 0);
  65.     }
  66.  
  67.     public int size () {
  68.         // Ja vrakja dolzinata na redicata.
  69.         return length;
  70.     }
  71.  
  72.     public E peek () {
  73.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  74.         if (front == null)
  75.             throw new NoSuchElementException();
  76.         return front.element;
  77.     }
  78.  
  79.     public void clear () {
  80.         // Ja prazni redicata.
  81.         front = rear = null;
  82.         length = 0;
  83.     }
  84.  
  85.     public void enqueue (E x) {
  86.         // Go dodava x na kraj od redicata.
  87.         SLLNode<E> latest = new SLLNode<E>(x, null);
  88.         if (rear != null) {
  89.             rear.succ = latest;
  90.             rear = latest;
  91.         } else
  92.             front = rear = latest;
  93.         length++;
  94.     }
  95.  
  96.     public E dequeue () {
  97.         // Go otstranuva i vrakja pochetniot element na redicata.
  98.         if (front != null) {
  99.             E frontmost = front.element;
  100.             front = front.succ;
  101.             if (front == null)  rear = null;
  102.             length--;
  103.             return frontmost;
  104.         } else
  105.             throw new NoSuchElementException();
  106.     }
  107.  
  108. }
  109.  
  110. class Gragjanin {
  111.     String namesurname;
  112.     int licna;
  113.     int pass;
  114.     int driving;
  115.     public Gragjanin(String name,int lKarta,int pasos,int vozacka)
  116.     {
  117.         namesurname = name;
  118.         licna = lKarta;
  119.         pass = pasos;
  120.         driving = vozacka;
  121.     }
  122.    
  123.     public boolean zavrsen()
  124.     {
  125.         return (licna == 0&&pass == 0 && driving == 0);
  126.     }
  127.  
  128.     public String getNamesurname() {
  129.         return namesurname;
  130.     }
  131.  
  132.     public void setNamesurname(String namesurname) {
  133.         this.namesurname = namesurname;
  134.     }
  135.  
  136.     public int getLicna() {
  137.         return licna;
  138.     }
  139.  
  140.     public void setLicna(int licna) {
  141.         this.licna = licna;
  142.     }
  143.  
  144.     public int getPass() {
  145.         return pass;
  146.     }
  147.  
  148.     public void setPass(int pass) {
  149.         this.pass = pass;
  150.     }
  151.  
  152.     public int getDriving() {
  153.         return driving;
  154.     }
  155.  
  156.     public void setDriving(int driving) {
  157.         this.driving = driving;
  158.     }
  159.    
  160. }
  161.  
  162. public class MVR {
  163.  
  164.     public static void main(String[] args) {
  165.        
  166.         Scanner br = new Scanner(System.in);
  167.        
  168.         int N = Integer.parseInt(br.nextLine());
  169.         LinkedQueue<Gragjanin> n1 = new LinkedQueue<Gragjanin>();
  170.         LinkedQueue<Gragjanin> n2 = new LinkedQueue<Gragjanin>();
  171.         LinkedQueue<Gragjanin> n3 = new LinkedQueue<Gragjanin>();
  172.        
  173.        
  174.         for(int i=1;i<=N;i++){
  175.             String imePrezime = br.nextLine();
  176.             int lKarta = Integer.parseInt(br.nextLine());
  177.             int pasos = Integer.parseInt(br.nextLine());
  178.             int vozacka = Integer.parseInt(br.nextLine());
  179.             Gragjanin covek = new Gragjanin(imePrezime,lKarta,pasos,vozacka);
  180.                 if(covek.getLicna() == 1)
  181.                     n1.enqueue(covek);
  182.                 else if(covek.getPass() == 1)
  183.                     n2.enqueue(covek);
  184.                 else if(covek.getDriving() == 1)
  185.                     n3.enqueue(covek);
  186.         }
  187.        
  188.         while(!n1.isEmpty())
  189.         {
  190.             Gragjanin pom = n1.dequeue();
  191.             pom.setLicna(0);
  192.             if(pom.getPass() == 1)
  193.                 n2.enqueue(pom);
  194.             else if(pom.getDriving() == 1)
  195.                 n3.enqueue(pom);
  196.            
  197.             if(pom.zavrsen())
  198.                 System.out.println(pom.getNamesurname());
  199.         }
  200.        
  201.         while(!n2.isEmpty())
  202.         {
  203.             Gragjanin pom = n2.dequeue();
  204.             pom.setPass(0);
  205.             if(pom.getDriving() == 1)
  206.                 n3.enqueue(pom);
  207.            
  208.             if(pom.zavrsen())
  209.                 System.out.println(pom.getNamesurname());
  210.         }
  211.        
  212.         while(!n3.isEmpty())
  213.         {
  214.             Gragjanin pom = n3.dequeue();
  215.             pom.setDriving(0);
  216.          if(pom.zavrsen())
  217.                 System.out.println(pom.getNamesurname());
  218.         }
  219.              
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement