Advertisement
Guest User

17.** Debugging Exercise: Be Positive

a guest
Oct 6th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class BePositive_broken
  6. {
  7.     public static void Main()
  8.     {
  9.         int countSequences = int.Parse(Console.ReadLine());
  10.  
  11.         for (int i = 0; i < countSequences; i++)
  12.         {
  13.             int[] input = Console.ReadLine().Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  14.  
  15.             List<int> data = new List<int>();
  16.  
  17.             for (int j = 0; j < input.Length - 1; j++)
  18.             {
  19.                 int currentNum = input[j];
  20.  
  21.                 if (currentNum >= 0)
  22.                 {
  23.                     data.Add(currentNum);
  24.                 }
  25.                 else
  26.                 {
  27.                     currentNum += input[j + 1];
  28.  
  29.                     if (currentNum >= 0)
  30.                     {
  31.                         data.Add(currentNum);
  32.                     }
  33.                     j++;
  34.                 }
  35.             }
  36.  
  37.             if (input.Length >= 2)
  38.             {
  39.                 if (input[input.Length - 2] >= 0 && input[input.Length - 1] >= 0)
  40.                 {
  41.                     data.Add(input[input.Length - 1]);
  42.                 }
  43.             }
  44.             else
  45.             {
  46.                 if (input[0] >= 0)
  47.                 {
  48.                     data.Add(input[0]);
  49.                 }
  50.             }
  51.  
  52.  
  53.             if (data.Count > 0)
  54.             {
  55.                 Console.WriteLine(string.Join(" ", data));
  56.             }
  57.             else
  58.             {
  59.                 Console.WriteLine("(empty)");
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement