Advertisement
xellscream

MaximalSumOfK

Jan 13th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2.  
  3. class MaximalSumOfK
  4. {
  5.     static void Main()
  6.     {
  7.  
  8.         int n, k, maxCount = 0, current = 0;
  9.         Console.Write("N= ");
  10.         n = int.Parse(Console.ReadLine());
  11.  
  12.         Console.Write("K= ");
  13.         k = int.Parse(Console.ReadLine());
  14.  
  15.         int[] arr = new int[n];
  16.         for(int index = 0; index < n; index++)
  17.         {
  18.             arr [index] = int.Parse(Console.ReadLine());
  19.         }
  20.        
  21.         for(int indexOne = 0, indexTwo = k-1, indexThree = 0; indexTwo < n; indexOne++, indexTwo++, indexThree++)
  22.         {
  23.             int count = 0;
  24.  
  25.             for(int index = 0; index < k; index++)
  26.             {
  27.                 count += arr[index + indexThree];
  28.             }
  29.             if(count > maxCount)
  30.             {
  31.                 maxCount = count;
  32.                 current = indexOne;
  33.             }
  34.             else
  35.             {
  36.                 continue;
  37.             }
  38.         }
  39.  
  40.         Console.Write("The biggest sum is " + maxCount + " between ");
  41.         for(int index = 0; index < k; index++)
  42.         {
  43.             Console.Write(arr[current]);
  44.             current++;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement