Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //juni 2022 - termin 2
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- public class Bank
- {
- public static void bank(SLL<Client> normal, SLL<Client> golden)
- {
- SLLNode<Client> temp1= golden.getFirst();
- SLLNode<Client> temp2= normal.getFirst();
- int min_importance= temp1.element.getLoyalty()*10+temp1.element.getAccounts()*20;
- int min_important_id= temp1.element.getId();
- temp1=temp1.succ;
- while(temp1!=null){
- int importance=temp1.element.getLoyalty()*10+temp1.element.getAccounts()*20;
- if(importance<min_importance){
- min_importance= importance;
- min_important_id= temp1.element.getId();
- }
- temp1=temp1.succ;
- }
- temp1= golden.getFirst();
- while(temp1!=null){
- if(temp1.element.getId()==min_important_id){
- normal.insertLast(new Client(temp1.element.getId(),temp1.element.getLoyalty(),temp1.element.getAccounts()));
- golden.delete(temp1);
- break;
- }
- temp1=temp1.succ;
- }
- int max_importance= temp2.element.getLoyalty()*10+temp2.element.getAccounts()*20;
- int max_important_id= temp2.element.getId();
- temp2=temp2.succ;
- while(temp2!=null){
- int importance=temp2.element.getLoyalty()*10+temp2.element.getAccounts()*20;
- if(importance>max_importance){
- max_importance=temp2.element.getLoyalty()*10+temp2.element.getAccounts()*20;
- max_important_id=temp2.element.getId();
- }
- temp2=temp2.succ;
- }
- temp2= normal.getFirst();
- while(temp2!=null){
- if(temp2.element.getId()==max_important_id){
- golden.insertLast(new Client(temp2.element.getId(),temp2.element.getLoyalty(),temp2.element.getAccounts()));
- normal.delete(temp2);
- break;
- }
- temp2=temp2.succ;
- }
- }
- public static void main(String[] args)
- {
- SLL<Client> Normal = new SLL<Client>();
- SLL<Client> Golden = new SLL<Client>();
- Scanner vnesi = new Scanner(System.in);
- int n = vnesi.nextInt();
- int m = vnesi.nextInt();
- for(int i=0;i<n;i++)
- {
- int id = vnesi.nextInt();
- int loyalty = vnesi.nextInt();
- int accounts = vnesi.nextInt();
- Client c1 = new Client(id,loyalty,accounts);
- Normal.insertLast(c1);
- }
- for(int j=0;j<m;j++)
- {
- int id = vnesi.nextInt();
- int loyalty = vnesi.nextInt();
- int accounts = vnesi.nextInt();
- Client c2 = new Client(id,loyalty,accounts);
- Golden.insertLast(c2);
- }
- bank(Normal,Golden);
- System.out.println(Normal.toString());
- System.out.println(Golden.toString());
- }
- }
- class Client
- {
- private int id;
- private int loyalty;
- private int accounts;
- public Client(int id, int loyalty, int accounts)
- {
- this.id = id;
- this.loyalty = loyalty;
- this.accounts = accounts;
- }
- public int getId() {
- return id;
- }
- public int getLoyalty() {
- return loyalty;
- }
- public int getAccounts() {
- return accounts;
- }
- @Override
- public String toString() {
- return String.valueOf(id);
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + " ";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + " ";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first==before){
- this.insertFirst(o);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first ==node){
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- //Test Case #1
- //3
- //2
- //80 35 2
- //90 40 1
- //60 50 20
- //15 10 2
- //14 25 4
- //
- //80 90 15
- //14 60
- //Test Case #2
- //3
- //2
- //56 20 3
- //24 10 2
- //55 20 3
- //12 20 4
- //13 20 4
- //56 24 55
- //13 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement