Martina312

Додавање елементи под просек

Feb 10th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ProsecenZbir {
  4.     public static float zbir(DLL<Integer> list){
  5.         DLLNode<Integer> tmp=list.getFirst();
  6.         float sum=0;
  7.  
  8.         while (tmp!=null){
  9.             sum+=tmp.element;
  10.             tmp=tmp.succ;
  11.         }
  12.         return sum/list.length();
  13.     }
  14.  
  15.     public static void result(DLL<Integer> list1, DLL<Integer> list2){
  16.         DLL<Integer> rezultanta=new DLL<>();
  17.         DLLNode<Integer> tmp1=list1.getFirst();
  18.         DLLNode<Integer> tmp2=list2.getLast();
  19.  
  20.         float prosek1=zbir(list1);
  21.         float prosek2=zbir(list2);
  22.  
  23.         int sum1=0;
  24.         int sum2=0;
  25.  
  26.         while (true) {
  27.             sum1 += tmp1.element;
  28.             if (sum1 < prosek1+prosek2) {
  29.                 rezultanta.insertLast(tmp1.element);
  30.                 tmp1 = tmp1.succ;
  31.             } else {
  32.                     break;
  33.             }
  34.         }
  35.  
  36.         while (true){
  37.             sum2+=tmp2.element;
  38.             if (sum2 < prosek2+prosek1) {
  39.                 rezultanta.insertLast(tmp2.element);
  40.                 tmp2 = tmp2.pred;
  41.             } else {
  42.                    break;
  43.             }
  44.         }
  45.         System.out.println(rezultanta);
  46.     }
  47.     public static void main(String[] args) {
  48.         Scanner in=new Scanner(System.in);
  49.  
  50.         int n1=in.nextInt();
  51.         DLL<Integer> list1=new DLL<>();
  52.         for(int i=0;i<n1;i++){
  53.             list1.insertLast(in.nextInt());
  54.         }
  55.  
  56.         int n2=in.nextInt();
  57.         DLL<Integer> list2=new DLL<>();
  58.         for(int i=0;i<n2;i++){
  59.             list2.insertLast(in.nextInt());
  60.         }
  61.         result(list1,list2);
  62.     }
  63. }
Add Comment
Please, Sign In to add comment