Martina312

Максимална подсума

Feb 11th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MaxPodsuma {
  4.     public static void maxPodsuma(SLL<Integer> list){
  5.         SLLNode<Integer> tmp=list.getFirst();
  6.         SLLNode<Integer> tmp2;
  7.         int sum=0;
  8.         int max=tmp.element;
  9.         while (tmp!=null){
  10.             sum=0;
  11.             sum+=tmp.element;
  12.             tmp2=tmp.succ;
  13.             while (tmp2!=null){
  14.                 sum+=tmp2.element;
  15.                 if(sum>max){
  16.                     max=sum;
  17.                 }
  18.                 tmp2=tmp2.succ;
  19.             }
  20.             tmp=tmp.succ;
  21.         }
  22.         System.out.println(max);
  23.     }
  24.     public static void main(String[] args) {
  25.         Scanner in=new Scanner(System.in);
  26.         int n=in.nextInt();
  27.  
  28.         SLL<Integer> list=new SLL<>();
  29.         for(int i=0;i<n;i++){
  30.             list.insertLast(in.nextInt());
  31.         }
  32.         maxPodsuma(list);
  33.     }
  34. }
Add Comment
Please, Sign In to add comment