Advertisement
Guest User

BePositive

a guest
Jan 27th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class _03_BePositiveCorrect {
  5.     public static void main(String[] args) {
  6.         @SuppressWarnings("resource")
  7.         Scanner scn = new Scanner(System.in);
  8.  
  9.         int countSequences = scn.nextInt();
  10.         scn.nextLine();
  11.  
  12.         for (int i = 0; i < countSequences; i++) {
  13.             String[] input = scn.nextLine().trim().split("\\s+");
  14.             ArrayList<Integer> numbers = new ArrayList<>();
  15.  
  16.             for (int j = 0; j < input.length; j++) {
  17.                 if (input[j].equals("") == false) {
  18.                     int num = Integer.parseInt(input[j]);
  19.                     numbers.add(num);
  20.                 }
  21.             }
  22.  
  23.             boolean found = false;
  24.  
  25.             for (int j = 0; j < numbers.size(); j++) {
  26.                 int currentNum = numbers.get(j);
  27.  
  28.                 if (currentNum >= 0) {
  29.                     System.out.printf("%d%s", currentNum,
  30.                             j == numbers.size() - 1 ? "" : " ");
  31.                     found = true;
  32.                 } else {
  33.                     if (j + 1 < numbers.size()) {
  34.                         currentNum += numbers.get(j + 1);
  35.                         j++;
  36.                         if (currentNum >= 0) {
  37.                             System.out.printf("%d%s", currentNum,
  38.                                     j == numbers.size() - 1 ? "" : " ");
  39.                             found = true;
  40.                         }
  41.  
  42.                     }
  43.                 }
  44.             }
  45.  
  46.             if (!found) {
  47.                 System.out.println("(empty)");
  48.             }
  49.             System.out.println();
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement