Martina312

Избриши N пати средина

Feb 4th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class NPatiSredina {
  4.     public static SLLNode<Integer> findEl(SLL<Integer> list, int n){
  5.         SLLNode<Integer> tmp=list.getFirst();
  6.         while(n!=0){
  7.             n--;
  8.             tmp=tmp.succ;
  9.         }
  10.         return tmp;
  11.     }
  12.     public static void brishi(SLL<Integer> list, int m){
  13.         while(m!=0){
  14.             m--;
  15.             int n=list.length()/2;
  16.  
  17.             if(list.length()%2!=0){
  18.                 list.delete(findEl(list,n));
  19.             }
  20.             else{
  21.                 SLLNode<Integer> first=findEl(list,n-1);
  22.                 SLLNode<Integer> second=findEl(list,n);
  23.                 if(first.element==second.element){
  24.                     list.delete(first);
  25.                 }
  26.                 else if(first.element<second.element){
  27.                     list.delete(first);
  28.                 }else{
  29.                     list.delete(second);
  30.                 }
  31.             }
  32.         }
  33.         System.out.println(list);
  34.     }
  35.     public static void main(String[] args) {
  36.         Scanner in=new Scanner(System.in);
  37.  
  38.         int n=in.nextInt();
  39.         SLL<Integer> list=new SLL<>();
  40.         for(int i=0;i<n;i++){
  41.             list.insertLast(in.nextInt());
  42.         }
  43.         int m=in.nextInt();
  44.  
  45.         brishi(list,m);
  46.     }
  47. }
Add Comment
Please, Sign In to add comment