Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2021 septemvri - termin 1
- import java.util.Scanner;
- class Matematicar {
- private int id;
- private double points;
- public Matematicar(int id, double points) {
- this.id = id;
- this.points = points;
- }
- public int getId() {
- return id;
- }
- public double getPoints() {
- return points;
- }
- @Override
- public String toString() {
- return String.valueOf(id);
- }
- }
- 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();
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public 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;
- }
- 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;
- }
- }
- public class MathCompetition {
- //todo: implement function
- public static void competition(SLL<Matematicar> prevYear, SLL<Matematicar> currYear) {
- SLLNode<Matematicar> temp1= prevYear.getFirst();
- SLLNode<Matematicar> temp2= currYear.getFirst();
- double prosecni_poeni, suma=0;
- int broj_matematicari= prevYear.length();
- while(temp1!=null){
- suma+= temp1.element.getPoints();
- temp1=temp1.succ;
- }
- prosecni_poeni=suma/(double)broj_matematicari;
- while(temp2!=null){
- if(temp2.element.getPoints()<prosecni_poeni){
- currYear.delete(temp2);
- }
- temp2=temp2.succ;
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int prevYearCount = Integer.parseInt(scanner.nextLine());
- int currYearCount = Integer.parseInt(scanner.nextLine());
- SLL<Matematicar> prevYear = new SLL<Matematicar>();
- SLL<Matematicar> currYear = new SLL<Matematicar>();
- for (int i = 0; i < prevYearCount; i++) {
- String line = scanner.nextLine();
- String[] parts = line.split("\\s+");
- prevYear.insertLast(new Matematicar(Integer.parseInt(parts[0]), Double.parseDouble(parts[1])));
- }
- for (int i = 0; i < currYearCount; i++) {
- String line = scanner.nextLine();
- String[] parts = line.split("\\s+");
- currYear.insertLast(new Matematicar(Integer.parseInt(parts[0]), Double.parseDouble(parts[1])));
- }
- competition(prevYear, currYear);
- System.out.println(currYear.toString());
- }
- }
- /*
- Test1
- 2
- 2
- 825 10.65
- 484 68.21
- 773 70.12
- 789 23.90
- 773
- Test2
- 1
- 8
- 27 85.96
- 526 29.64
- 150 11.57
- 730 37.12
- 58 77.79
- 662 40.35
- 380 59.98
- 77 37.55
- 321 34.47
- Prazna Lista!!!
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement