Advertisement
dzocesrce

[APS] Forum

Aug 27th, 2023
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.88 KB | None | 0 0
  1. // 2022 januari - termin 2
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Discussion {
  6.     private int id;
  7.     private int popularity;
  8.     private int users;
  9.  
  10.     public Discussion(int id, int popularity, int users) {
  11.         this.id = id;
  12.         this.popularity = popularity;
  13.         this.users = users;
  14.     }
  15.  
  16.     public int getId() {
  17.         return id;
  18.     }
  19.  
  20.     public void setId(int id) {
  21.         this.id = id;
  22.     }
  23.  
  24.     public int getPopularity() {
  25.         return popularity;
  26.     }
  27.  
  28.     public void setPopularity(int popularity) {
  29.         this.popularity = popularity;
  30.     }
  31.  
  32.     public int getUsers() {
  33.         return users;
  34.     }
  35.  
  36.     public void setUsers(int users) {
  37.         this.users = users;
  38.     }
  39.  
  40.     @Override
  41.     public String toString() {
  42.         return String.valueOf(id);
  43.     }
  44. }
  45.  
  46. class SLLNode<E> {
  47.     protected E element;
  48.     protected SLLNode<E> succ;
  49.  
  50.     public SLLNode(E elem, SLLNode<E> succ) {
  51.         this.element = elem;
  52.         this.succ = succ;
  53.     }
  54.  
  55.     @Override
  56.     public String toString() {
  57.         return element.toString();
  58.     }
  59. }
  60.  
  61. class SLL<E> {
  62.     private SLLNode<E> first;
  63.  
  64.     public SLL() {
  65.         this.first = null;
  66.     }
  67.  
  68.     public void deleteList() {
  69.         first = null;
  70.     }
  71.  
  72.     public int length() {
  73.         int ret;
  74.         if (first != null) {
  75.             SLLNode<E> tmp = first;
  76.             ret = 1;
  77.             while (tmp.succ != null) {
  78.                 tmp = tmp.succ;
  79.                 ret++;
  80.             }
  81.             return ret;
  82.         } else
  83.             return 0;
  84.  
  85.     }
  86.  
  87.     @Override
  88.     public String toString() {
  89.         String ret = new String();
  90.         if (first != null) {
  91.             SLLNode<E> tmp = first;
  92.             ret += tmp;
  93.             while (tmp.succ != null) {
  94.                 tmp = tmp.succ;
  95.                 ret += " " + tmp;
  96.             }
  97.         } else
  98.             ret = "Prazna lista!!!";
  99.         return ret;
  100.     }
  101.  
  102.     public void insertFirst(E o) {
  103.         SLLNode<E> ins = new SLLNode<E>(o, first);
  104.         first = ins;
  105.     }
  106.  
  107.     public void insertAfter(E o, SLLNode<E> node) {
  108.         if (node != null) {
  109.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  110.             node.succ = ins;
  111.         } else {
  112.             System.out.println("Dadenot jazol e null");
  113.         }
  114.     }
  115.  
  116.     public void insertBefore(E o, SLLNode<E> before) {
  117.         if (first != null) {
  118.             SLLNode<E> tmp = first;
  119.             if (first == before) {
  120.                 this.insertFirst(o);
  121.                 return;
  122.             }
  123.             while (tmp.succ != before)
  124.                 tmp = tmp.succ;
  125.             if (tmp.succ == before) {
  126.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  127.                 tmp.succ = ins;
  128.             } else {
  129.                 System.out.println("Elementot ne postoi vo listata");
  130.             }
  131.         } else {
  132.             System.out.println("Listata e prazna");
  133.         }
  134.     }
  135.  
  136.     public void insertLast(E o) {
  137.         if (first != null) {
  138.             SLLNode<E> tmp = first;
  139.             while (tmp.succ != null)
  140.                 tmp = tmp.succ;
  141.             SLLNode<E> ins = new SLLNode<E>(o, null);
  142.             tmp.succ = ins;
  143.         } else {
  144.             insertFirst(o);
  145.         }
  146.     }
  147.  
  148.     public E deleteFirst() {
  149.         if (first != null) {
  150.             SLLNode<E> tmp = first;
  151.             first = first.succ;
  152.             return tmp.element;
  153.         } else {
  154.             System.out.println("Listata e prazna");
  155.             return null;
  156.         }
  157.     }
  158.  
  159.     public E delete(SLLNode<E> node) {
  160.         if (first != null) {
  161.             SLLNode<E> tmp = first;
  162.             if (first == node) {
  163.                 return this.deleteFirst();
  164.             }
  165.             while (tmp.succ != node && tmp.succ.succ != null)
  166.                 tmp = tmp.succ;
  167.             if (tmp.succ == node) {
  168.                 tmp.succ = tmp.succ.succ;
  169.                 return node.element;
  170.             } else {
  171.                 System.out.println("Elementot ne postoi vo listata");
  172.                 return null;
  173.             }
  174.         } else {
  175.             System.out.println("Listata e prazna");
  176.             return null;
  177.         }
  178.     }
  179.  
  180.     public SLLNode<E> getFirst() {
  181.         return first;
  182.     }
  183.  
  184.     public SLLNode<E> find(E o) {
  185.         if (first != null) {
  186.             SLLNode<E> tmp = first;
  187.             while (tmp.element != o && tmp.succ != null)
  188.                 tmp = tmp.succ;
  189.             if (tmp.element == o) {
  190.                 return tmp;
  191.             } else {
  192.                 System.out.println("Elementot ne postoi vo listata");
  193.             }
  194.         } else {
  195.             System.out.println("Listata e prazna");
  196.         }
  197.         return first;
  198.     }
  199. }
  200.  
  201. public class Forum {
  202.  
  203.     // TODO: implement function
  204.     public static void forum(SLL<Discussion> health, SLL<Discussion> finance){
  205.         SLLNode<Discussion> temp1= health.getFirst();
  206.         SLLNode<Discussion> temp2= finance.getFirst();
  207.         int least_important= temp2.element.getPopularity()*temp2.element.getUsers();
  208.         int least_important_id= temp2.element.getId();
  209.         temp2=temp2.succ;
  210.         while(temp2!=null){
  211.             int importance= temp2.element.getPopularity()*temp2.element.getUsers();
  212.             if(importance<least_important){
  213.                 least_important=importance;
  214.                 least_important_id= temp2.element.getId();
  215.             }
  216.             temp2=temp2.succ;
  217.         }
  218.         temp2= finance.getFirst();
  219.         while(true){
  220.             if(least_important_id==temp2.element.getId()){
  221.                 finance.delete(temp2);
  222.                 break;
  223.             }
  224.             temp2=temp2.succ;
  225.         }
  226.         int most_important= temp1.element.getPopularity()*temp1.element.getUsers();
  227.         int most_important_id= temp1.element.getId();
  228.         int most_important_popularaity= temp1.element.getPopularity();
  229.         int most_important_users= temp1.element.getUsers();
  230.         temp1= temp1.succ;
  231.         while(temp1!=null){
  232.             int importance= temp1.element.getPopularity()*temp1.element.getUsers();
  233.             if(importance>most_important){
  234.                 most_important=importance;
  235.                 most_important_id= temp1.element.getId();
  236.                 most_important_popularaity= temp1.element.getPopularity();
  237.                 most_important_users= temp1.element.getUsers();
  238.             }
  239.             temp1=temp1.succ;
  240.         }
  241.         finance.insertLast(new Discussion(most_important_id,most_important_popularaity,most_important_users));
  242.     }
  243.  
  244.  
  245.     public static void main(String[] args) {
  246.         Scanner scanner = new Scanner(System.in);
  247.         int numHealth = Integer.parseInt(scanner.nextLine());
  248.         int numFinance = Integer.parseInt(scanner.nextLine());
  249.  
  250.         SLL<Discussion> health = new SLL<Discussion>();
  251.         SLL<Discussion> finance = new SLL<Discussion>();
  252.  
  253.         for (int i = 0; i < numHealth; i++) {
  254.             String line = scanner.nextLine();
  255.             String[] parts = line.split("\\s+");
  256.             health.insertLast(new Discussion(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
  257.         }
  258.  
  259.         for (int i = 0; i < numFinance; i++) {
  260.             String line = scanner.nextLine();
  261.             String[] parts = line.split("\\s+");
  262.             finance.insertLast(new Discussion(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
  263.         }
  264.  
  265.         forum(health, finance);
  266.         System.out.println(health.toString());
  267.         System.out.println(finance.toString());
  268.     }
  269. }
  270. /*
  271. Влез:
  272. 2
  273. 2
  274. 111 80 12
  275. 222 70 11
  276. 333 50 10
  277. 444 90 77
  278.  
  279. Излез:
  280. 111 222
  281. 444 111
  282.  
  283. Влез:
  284. 3
  285. 3
  286. 234 58 100
  287. 663 59 3
  288. 825 18 50
  289. 256 48 55
  290. 202 80 44
  291. 242 93 74
  292.  
  293. Излез:
  294. 234 663 825
  295. 202 242 234
  296.  
  297. Влез:
  298. 1
  299. 1
  300. 100 11 21
  301. 791 2 18
  302.  
  303. Излез:
  304. 100
  305. 100
  306. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement