Advertisement
duplicityyy

[АПС] - Листа од Листи

Aug 15th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. Дадена е двојно поврзана листа од двојно поврзани листи. Да се најде сума на секоја од подлистите, а потоа производ на овие суми
  2.  
  3. Влез: Број N кој кажува колку листи има Број М кој кажува колку елементи има во секоја листа Во следните М линии се податоците 1<=A<=1000за секоја од листите
  4.  
  5. Излез: Еден број што е производот на сумите од низите. Со седум децимали.
  6.  
  7. Пример влез: 3 4 1 2 3 4 2 3 4 5 6 7 8 9
  8.  
  9. Излез: 1400
  10.  
  11. Sample Input
  12. 4
  13. 4
  14. 6 13 16 7
  15. 7 23 9 11
  16. 8 0 8 19
  17. 6 6 9 11
  18.  
  19. Sample Output
  20. 2352000
  21.  
  22. Sample Input
  23. 7
  24. 18
  25. 22 6 23 1 9 1 14 11 23 5 13 0 18 5 0 18 6 24
  26. 15 7 15 0 8 0 5 8 6 20 21 23 18 21 13 9 0 12
  27. 12 24 1 4 19 24 17 0 23 9 18 13 15 2 6 5 6 23
  28. 13 1 0 2 17 7 15 16 15 20 1 11 9 16 21 13 15 15
  29. 19 5 9 23 6 2 14 9 13 18 12 8 2 1 18 23 4 21
  30. 19 13 1 13 7 6 2 8 2 5 5 14 11 11 3 23 3 16
  31. 1 16 16 10 21 17 22 9 15 3 14 14 8 1 6 10 7 20
  32.  
  33. Sample Output
  34. 12885948986421420
  35. ---------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. import java.util.Scanner;
  38.  
  39. class DLLNode<E> {
  40.     protected E element;
  41.     protected DLLNode<E> pred, succ;
  42.  
  43.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  44.         this.element = elem;
  45.         this.pred = pred;
  46.         this.succ = succ;
  47.     }
  48.  
  49.     @Override
  50.     public String toString() {
  51.         return "<-" + element.toString() + "->";
  52.     }
  53. }
  54.  
  55. class DLL<E> {
  56.     private DLLNode<E> first, last;
  57.  
  58.     public DLL() {
  59.         // Construct an empty SLL
  60.         this.first = null;
  61.         this.last = null;
  62.     }
  63.  
  64.     public void deleteList() {
  65.         first = null;
  66.         last = null;
  67.     }
  68.  
  69.     public int length() {
  70.         int ret;
  71.         if (first != null) {
  72.             DLLNode<E> tmp = first;
  73.             ret = 1;
  74.             while (tmp.succ != null) {
  75.                 tmp = tmp.succ;
  76.                 ret++;
  77.             }
  78.             return ret;
  79.         } else
  80.             return 0;
  81.  
  82.     }
  83.  
  84.     public void insertFirst(E o) {
  85.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  86.         if (first == null)
  87.             last = ins;
  88.         else
  89.             first.pred = ins;
  90.         first = ins;
  91.     }
  92.  
  93.     public void insertLast(E o) {
  94.         if (first == null)
  95.             insertFirst(o);
  96.         else {
  97.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  98.             last.succ = ins;
  99.             last = ins;
  100.         }
  101.     }
  102.  
  103.     public void insertAfter(E o, DLLNode<E> after) {
  104.         if (after == last) {
  105.             insertLast(o);
  106.             return;
  107.         }
  108.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  109.         after.succ.pred = ins;
  110.         after.succ = ins;
  111.     }
  112.  
  113.     public void insertBefore(E o, DLLNode<E> before) {
  114.         if (before == first) {
  115.             insertFirst(o);
  116.             return;
  117.         }
  118.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  119.         before.pred.succ = ins;
  120.         before.pred = ins;
  121.     }
  122.  
  123.     public E deleteFirst() {
  124.         if (first != null) {
  125.             DLLNode<E> tmp = first;
  126.             first = first.succ;
  127.             if (first != null) first.pred = null;
  128.             if (first == null)
  129.                 last = null;
  130.             return tmp.element;
  131.         } else
  132.             return null;
  133.     }
  134.  
  135.     public E deleteLast() {
  136.         if (first != null) {
  137.             if (first.succ == null)
  138.                 return deleteFirst();
  139.             else {
  140.                 DLLNode<E> tmp = last;
  141.                 last = last.pred;
  142.                 last.succ = null;
  143.                 return tmp.element;
  144.             }
  145.         }
  146.         // else throw Exception
  147.         return null;
  148.     }
  149.  
  150.     @Override
  151.     public String toString() {
  152.         String ret = new String();
  153.         if (first != null) {
  154.             DLLNode<E> tmp = first;
  155.             ret += tmp + "<->";
  156.             while (tmp.succ != null) {
  157.                 tmp = tmp.succ;
  158.                 ret += tmp + "<->";
  159.             }
  160.         } else
  161.             ret = "Prazna lista!!!";
  162.         return ret;
  163.     }
  164.  
  165.     public DLLNode<E> getFirst() {
  166.         return first;
  167.     }
  168.  
  169.     public DLLNode<E> getLast() {
  170.  
  171.         return last;
  172.     }
  173.  
  174. }
  175.  
  176. public class ListaOdListi {
  177.  
  178.     public static long findMagicNumber(DLL<DLL<Integer>> list) {
  179.         long proizvod = 1;
  180.         DLLNode<DLL<Integer>> marker = list.getFirst();
  181.         while (marker != null) {
  182.             DLLNode<Integer> brojach = marker.element.getFirst();
  183.             long suma = 0;
  184.             while (brojach != null) {
  185.                 suma += brojach.element;
  186.                 brojach = brojach.succ;
  187.             }
  188.             marker = marker.succ;
  189.             proizvod *= suma;
  190.         }
  191.  
  192.         return proizvod;
  193.     }
  194.  
  195.     public static void main(String[] args) {
  196.         Scanner in = new Scanner(System.in);
  197.         int n = in.nextInt();
  198.         int m = in.nextInt();
  199.         DLL<DLL<Integer>> list = new DLL<DLL<Integer>>();
  200.         for (int i = 0; i < n; i++) {
  201.             DLL<Integer> tmp = new DLL<Integer>();
  202.             for (int j = 0; j < m; j++) {
  203.                 tmp.insertLast(in.nextInt());
  204.             }
  205.             list.insertLast(tmp);
  206.         }
  207.         in.close();
  208.         System.out.println(findMagicNumber(list));
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement