Advertisement
Naralex

Arrays-SequenceOfMaximalSum

Jan 14th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2.  
  3. class SequenceOfMaximalSum
  4. {
  5.     static void Main()
  6.     {
  7.  
  8.     //  08.Write a program that finds the sequence of maximal sum in given array.
  9.     //Example: {2, 3, -6, -1, 2, -1, 6, 4, -8, 8}  {2, -1, 6, 4}
  10.     //Can you do it with only one loop (with single scan through the elements of the array)?
  11.  
  12.  
  13.         Console.Write("Type in the size of the Array: ");
  14.         int arraySize = int.Parse(Console.ReadLine());
  15.         int[] arr = new int[arraySize];
  16.  
  17.         int count = 1, number;
  18.         string curentSequence = " ", endSequence = " ";
  19.  
  20.         for (int index = 0; index < arraySize; index++)
  21.         {
  22.             Console.Write("Position {0} : ", index + 1);
  23.             number = int.Parse(Console.ReadLine());
  24.             arr[index] = number;
  25.         }
  26.      
  27.         //int[] arr = new int[] { 2, 3, -6, -1, 2, -1, 6, 4, -8, 8 };
  28.  
  29.         for (int i = 0; i < arr.Length -1; i++)
  30.         {
  31.             if (arr[i] > 0)
  32.             {
  33.                 if (arr[i] + arr[i + 1] > 0)
  34.                 {
  35.                     count++;
  36.                     curentSequence += arr[i] + " ";
  37.                     endSequence = curentSequence;
  38.                 }
  39.                 if (arr[i] + arr[i + 1] < 0)
  40.                 {
  41.                     if (count > 1)
  42.                     {
  43.                         count = 1;
  44.                         curentSequence += arr[i] + " ";
  45.                         endSequence = curentSequence;
  46.                     }
  47.                     if (count == 1)
  48.                     {
  49.                         count = 1;
  50.                         curentSequence = " ";
  51.                     }
  52.                 }
  53.             }
  54.             if (arr[i] < 0)
  55.             {
  56.                 if (arr[i] + arr[i + 1] > 0)
  57.                 {
  58.                     if (count == 1)
  59.                     {
  60.                         curentSequence = " ";
  61.                     }
  62.                     if (count > 1)
  63.                     {
  64.                         count++;
  65.                         curentSequence += arr[i] + " ";
  66.                         endSequence = curentSequence;
  67.                     }
  68.                 }
  69.                 if (arr[i] + arr[i + 1] < 0)
  70.                 {
  71.                     if (count == 1)
  72.                     {
  73.                         curentSequence = " ";
  74.                     }
  75.                     if (count > 1)
  76.                     {
  77.                         count = 1;
  78.                         curentSequence += arr[i] + " ";
  79.                         endSequence = curentSequence;
  80.                     }
  81.                 }    
  82.             }
  83.         }
  84.         Console.WriteLine(endSequence);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement