Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MaxSum
- {
- static void Main()
- {
- int maxElement = int.MinValue;
- int index = 0;
- uint arrayLength = 0;
- int maxSum = 0;
- Console.Write("Enter the arrays's length: ");
- while (!uint.TryParse(Console.ReadLine(), out arrayLength))
- {
- Console.Write("Invalid input. Enter a positive integer number: ");
- }
- int elementsCount = 0;
- Console.Write("Enter the count of the elements to be summed: ");
- while (!int.TryParse(Console.ReadLine(), out elementsCount) || elementsCount > arrayLength)
- {
- Console.Write("Invalid input. Enter a positive integer number: ");
- }
- int[] array = new int[arrayLength];
- for (int i = 0; i < arrayLength; i++)
- {
- Console.Write("Enter the {0} element of the array: ", i);
- while (!int.TryParse(Console.ReadLine(), out array[i]))
- {
- Console.Write("Invalid input. Enter an integer number: ");
- }
- }
- int[] maxElements = new int[elementsCount];
- for (int i = 0; i < elementsCount; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- if (array[j] > maxElement)
- {
- maxElement = array[j];
- index = j;
- }
- }
- maxElements[elementsCount - 1 - i] = maxElement;
- maxSum += maxElements[elementsCount - 1 - i];
- maxElement = int.MinValue;
- array[index] = int.MinValue;
- }
- Console.WriteLine("The maximal sum of {0} elements is the sum of the elements {1}, which is {2}.", elementsCount, string.Join(", ", maxElements), maxSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement