Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- interface Queue<E> {
- // Elementi na redicata se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako redicata e prazena.
- public int size ();
- // Ja vrakja dolzinata na redicata.
- public E peek ();
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- // Metodi za transformacija:
- public void clear ();
- // Ja prazni redicata.
- public void enqueue (E x);
- // Go dodava x na kraj od redicata.
- public E dequeue ();
- // Go otstranuva i vrakja pochetniot element na redicata.
- }
- class ArrayQueue<E> implements Queue<E> {
- // Redicata e pretstavena na sledniot nacin:
- // length go sodrzi brojot na elementi.
- // Ako length > 0, togash elementite na redicata se zachuvani vo elems[front...rear-1]
- // Ako rear > front, togash vo elems[front...maxlength-1] i elems[0...rear-1]
- E[] elems;
- int length, front, rear;
- // Konstruktor ...
- @SuppressWarnings("unchecked")
- public ArrayQueue (int maxlength) {
- elems = (E[]) new Object[maxlength];
- clear();
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako redicata e prazena.
- return (length == 0);
- }
- public int size () {
- // Ja vrakja dolzinata na redicata.
- return length;
- }
- public E peek () {
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- if (length > 0)
- return elems[front];
- else
- throw new NoSuchElementException();
- }
- public void clear () {
- // Ja prazni redicata.
- length = 0;
- front = rear = 0; // arbitrary
- }
- public void enqueue (E x) {
- // Go dodava x na kraj od redicata.
- elems[rear++] = x;
- if (rear == elems.length) rear = 0;
- length++;
- }
- public E dequeue () {
- // Go otstranuva i vrakja pochetniot element na redicata.
- if (length > 0) {
- E frontmost = elems[front];
- elems[front++] = null;
- if (front == elems.length) front = 0;
- length--;
- return frontmost;
- } else
- throw new NoSuchElementException();
- }
- }
- class Gragjanin {
- private String imePrezime;
- private int lKarta;
- private int pasosh;
- private int vozacka;
- public Gragjanin(String imePrezime, int lKarta, int pasosh, int vozacka) {
- this.imePrezime = imePrezime;
- this.lKarta = lKarta;
- this.pasosh = pasosh;
- this.vozacka = vozacka;
- }
- public String getImePrezime() {
- return imePrezime;
- }
- public int getlKarta() {
- return lKarta;
- }
- public int getPasosh() {
- return pasosh;
- }
- public int getVozacka() {
- return vozacka;
- }
- }
- public class MVR {
- public static void main(String[] args) {
- Scanner br = new Scanner(System.in);
- int N = Integer.parseInt(br.nextLine());
- ArrayQueue<Gragjanin> firstQueue = new ArrayQueue<>(N); // red za lichna karta
- ArrayQueue<Gragjanin> secondQueue = new ArrayQueue<>(N); // red za pasosh
- ArrayQueue<Gragjanin> thirdQueue = new ArrayQueue<>(N); // red za vozachka
- for(int i=1;i<=N;i++){
- String imePrezime = br.nextLine();
- int lKarta = Integer.parseInt(br.nextLine());
- int pasos = Integer.parseInt(br.nextLine());
- int vozacka = Integer.parseInt(br.nextLine());
- Gragjanin covek = new Gragjanin(imePrezime,lKarta,pasos,vozacka);
- if(lKarta == 1) firstQueue.enqueue(covek);
- if(pasos == 1 && lKarta == 0) secondQueue.enqueue(covek);
- if(vozacka == 1 && pasos == 0 && lKarta == 0) thirdQueue.enqueue(covek);
- }
- while(!firstQueue.isEmpty()) {
- Gragjanin covek = firstQueue.dequeue();
- if(covek.getPasosh() == 1) secondQueue.enqueue(covek);
- else if(covek.getVozacka() == 1) thirdQueue.enqueue(covek);
- else System.out.println(covek.getImePrezime());
- }
- while(!secondQueue.isEmpty()) {
- Gragjanin covek = secondQueue.dequeue();
- if(covek.getVozacka() == 1) thirdQueue.enqueue(covek);
- else System.out.println(covek.getImePrezime());
- }
- while(!thirdQueue.isEmpty()) {
- System.out.println(thirdQueue.dequeue().getImePrezime());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement