Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sing System;
- class MaximalSum
- {
- static void Main()
- {
- //Write a program that reads two integer numbers N and K and an array of N elements from the console.
- //Find in the array those K elements that have maximal sum.
- Console.WriteLine("Enter array's length: N = ");
- int n = int.Parse(Console.ReadLine());
- int[] array=new int[n];
- for (int i = 0; i < n; i++)
- {
- Console.WriteLine("Enter number: ");
- array[i] = int.Parse(Console.ReadLine());
- }
- Console.WriteLine("Enter sequance's length with maximal sum: K = ");
- int k = int.Parse(Console.ReadLine());
- int maxNumber = 0;
- int currentNumber = 0;
- for (int j = 0; j < k; j++)
- {
- for (int i = 0; i < n; i++)
- {
- if (array[i] > maxNumber)
- {
- if (array[i] != currentNumber)
- {
- maxNumber = array[i];
- }
- }
- }
- currentNumber = maxNumber;
- maxNumber = 0;
- Console.WriteLine(currentNumber);
- for (int m = 0; m < n; m++)
- {
- if (array[m] == currentNumber)
- {
- array[m] = 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment