Advertisement
Guest User

Untitled

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