Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SequenceOfMaximalSum
- {
- static void Main()
- {
- // 08.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}
- //Can you do it with only one loop (with single scan through the elements of the array)?
- Console.Write("Type in the size of the Array: ");
- int arraySize = int.Parse(Console.ReadLine());
- int[] arr = new int[arraySize];
- int count = 1, number;
- string curentSequence = " ", endSequence = " ";
- for (int index = 0; index < arraySize; index++)
- {
- Console.Write("Position {0} : ", index + 1);
- number = int.Parse(Console.ReadLine());
- arr[index] = number;
- }
- //int[] arr = new int[] { 2, 3, -6, -1, 2, -1, 6, 4, -8, 8 };
- for (int i = 0; i < arr.Length -1; i++)
- {
- if (arr[i] > 0)
- {
- if (arr[i] + arr[i + 1] > 0)
- {
- count++;
- curentSequence += arr[i] + " ";
- endSequence = curentSequence;
- }
- if (arr[i] + arr[i + 1] < 0)
- {
- if (count > 1)
- {
- count = 1;
- curentSequence += arr[i] + " ";
- endSequence = curentSequence;
- }
- if (count == 1)
- {
- count = 1;
- curentSequence = " ";
- }
- }
- }
- if (arr[i] < 0)
- {
- if (arr[i] + arr[i + 1] > 0)
- {
- if (count == 1)
- {
- curentSequence = " ";
- }
- if (count > 1)
- {
- count++;
- curentSequence += arr[i] + " ";
- endSequence = curentSequence;
- }
- }
- if (arr[i] + arr[i + 1] < 0)
- {
- if (count == 1)
- {
- curentSequence = " ";
- }
- if (count > 1)
- {
- count = 1;
- curentSequence += arr[i] + " ";
- endSequence = curentSequence;
- }
- }
- }
- }
- Console.WriteLine(endSequence);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement