Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //laboratoriska, se bara proizvod na sumi na podlisti od lista!
- import java.util.Scanner;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public DLLNode<E> find(E o) {
- if (first != null) {
- DLLNode<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 void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if(after==last){
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if(before == first){
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- public E delete(DLLNode<E> node) {
- if(node==first){
- deleteFirst();
- return node.element;
- }
- if(node==last){
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- if(tmp.succ==null){
- ret+=tmp;
- break;
- }
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public String toStringR() {
- String ret = new String();
- if (last != null) {
- DLLNode<E> tmp = last;
- ret += tmp + "<->";
- while (tmp.pred != null) {
- tmp = tmp.pred;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void izvadiDupliIPrebroj(){
- }
- }
- public class ListOfListsProduct {
- public static long get_product(DLL<DLL<Integer>> list){
- DLLNode<DLL<Integer>> temp= list.getFirst();
- long sum=0, product=1;
- while(temp!=null){
- sum=0;
- DLLNode<Integer> temp2= temp.element.getFirst();
- while(temp2!=null){
- sum+=temp2.element;
- temp2=temp2.succ;
- }
- product*=sum;
- temp=temp.succ;
- }
- return product;
- }
- public static void main(String[] args) {
- Scanner input= new Scanner(System.in);
- int n= Integer.parseInt(input.nextLine());
- int m= Integer.parseInt(input.nextLine());
- DLL<DLL<Integer>> list_of_lists= new DLL<>();
- for(int i=0;i<n;i++){
- DLL<Integer> list= new DLL<>();
- String[] broevi= input.nextLine().split("\\s+");
- for(int j=0;j<m;j++){
- list.insertLast(Integer.parseInt(broevi[j]));
- }
- list_of_lists.insertLast(list);
- }
- System.out.println(get_product(list_of_lists));
- }
- }
- /*
- 3
- 8
- 20 22 11 20 22 24 8 13
- 5 6 10 16 3 13 5 2
- 17 0 18 13 8 18 6 18
- 823200
- 7
- 18
- 22 6 23 1 9 1 14 11 23 5 13 0 18 5 0 18 6 24
- 15 7 15 0 8 0 5 8 6 20 21 23 18 21 13 9 0 12
- 12 24 1 4 19 24 17 0 23 9 18 13 15 2 6 5 6 23
- 13 1 0 2 17 7 15 16 15 20 1 11 9 16 21 13 15 15
- 19 5 9 23 6 2 14 9 13 18 12 8 2 1 18 23 4 21
- 19 13 1 13 7 6 2 8 2 5 5 14 11 11 3 23 3 16
- 1 16 16 10 21 17 22 9 15 3 14 14 8 1 6 10 7 20
- 12885948986421420
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement