Advertisement
Guest User

[C#] Домашно Arrays - 8 Задача

a guest
Dec 15th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. //Write a program that finds the sequence of maximal sum in given array. Example: {2, 3, -6, -1, 2, -1, 6, 4, -8, 8}  {2, -1, 6, 4}
  2.  
  3. using System;
  4.  
  5. class TheSequenceOfMaxSum
  6. {
  7.     static void Main()
  8.     {
  9.         string inputArrayOne = Console.ReadLine();
  10.         char[] delimiter = new char[] {',',' '};
  11.         string [] inputArrayTwo = inputArrayOne.Split(delimiter,StringSplitOptions.RemoveEmptyEntries);
  12.        
  13.         int[]realArray=new int[inputArrayTwo.Length];
  14.         int currentSum = 0;
  15.         int maxSum = 0;
  16.         int counter = 1;
  17.         int arrayNumber = 0;
  18.         for (int i = 0; i < inputArrayTwo.Length; i++)
  19.         {
  20.             realArray[i] = int.Parse(inputArrayTwo[i]);
  21.             currentSum += realArray[i];
  22.             if (currentSum > maxSum)
  23.             {
  24.                 maxSum = currentSum;
  25.                 counter++;
  26.                 arrayNumber = i;
  27.             }
  28.             if (currentSum < 0)
  29.             {
  30.                 currentSum = 0;
  31.                 counter = 1;
  32.             }
  33.             if (i == inputArrayTwo.Length - 1)
  34.             {
  35.                 for (int j = arrayNumber-counter; j <=arrayNumber; j++)
  36.                 {
  37.                     Console.Write(realArray[j]);
  38.                     if (j < arrayNumber)
  39.                     {
  40.                         Console.Write(", ");
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         Console.WriteLine();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement