Advertisement
Guest User

2.2

a guest
Sep 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. public static void main (String[] args) throws java.lang.Exception
  2.     {
  3.        
  4.         Scanner in = new Scanner(System.in);
  5.         String str = in.nextLine();
  6.         int sNum = in.nextInt();
  7.        
  8.         String parseStr[] = str.split(",");
  9.         Integer[] sequence = new Integer[parseStr.length];
  10.         int k=0;
  11.         for(String s : parseStr){
  12.             sequence[k]=Integer.parseInt(s.trim());
  13.             k++;
  14.         }
  15.        
  16.         int j = 0;
  17.         Boolean chk = true;
  18.         while (j < sequence.length - 1 && chk) {
  19.             int i = j;
  20.             int sum = 0;
  21.             while (i < sequence.length - 1 && sum <= sNum) {
  22.                 sum += sequence[i];
  23.                 if (sum == sNum) {
  24.                     for (int l = j; l <= i; l++) {
  25.                         System.out.print(sequence[l]);
  26.                         if (l < i)
  27.                             System.out.print(", ");
  28.                         else
  29.                             System.out.println("");
  30.                     }
  31.                     chk = false;
  32.                     break;
  33.                 }
  34.                 i++;
  35.             }
  36.             j++;
  37.         }
  38.        
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement