Venciity

[C#] BOOK Arrays - 7 Задача

Jan 6th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. /*Да се напише програма, която чете от конзолата две цели числа N и K (K<N), и масив от N елемента.
  2.  * Да се намерят тези K поредни елемента, които имат максимална сума. */
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _7_Exercise
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("Numbers = ");
  17.             int n = int.Parse(Console.ReadLine());
  18.             Console.Write("the average work = ");
  19.             int k = int.Parse(Console.ReadLine());
  20.             int[] array = new int[n];
  21.  
  22.             if (k > n)
  23.             {
  24.                 Console.WriteLine("N must be bigger than K!");
  25.                 return;
  26.             }
  27.  
  28.             for (int index = 0; index < n; index++)
  29.             {
  30.                 Console.Write("Number {0} = ",index+1);
  31.                 array[index] = int.Parse(Console.ReadLine());
  32.             }
  33.  
  34.             int sum = 0;
  35.             Array.Sort(array);
  36.  
  37.             for (int index = n - 1; index >= n - k; index--)
  38.             {
  39.                 sum += array[index];
  40.             }
  41.             Console.WriteLine();
  42.             Console.WriteLine("Max sum of {0} numbers is {1}",k,sum);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment