Advertisement
simonses

GivenSumOfSSeq

Jan 11th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2.  
  3. class GivenSumOfSSeq
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Please, Insert the length of the array: ");
  8.         int n = int.Parse(Console.ReadLine());
  9.  
  10.         Console.Write("Now the SUM which we find!: ");
  11.         int givenSum = int.Parse(Console.ReadLine());
  12.  
  13.         // Read the array
  14.         int[] array = new int[n];
  15.         for (int i = 0; i < n; i++)
  16.         {
  17.             array[i] = int.Parse(Console.ReadLine());
  18.         }
  19.  
  20.         // Check
  21.         int sum = 0;
  22.         int startCurent = 0;
  23.         int startPosition = 0;
  24.         int sumLength = 0;
  25.         int same = 0;
  26.         int countLength = 0;
  27.  
  28.         for (int i = 0; i < n; i++) // Get the start position
  29.         {
  30.             if (array[i] < givenSum)
  31.             {
  32.                 startCurent = i;
  33.                 sum = 0;
  34.                 countLength = 0;
  35.                 while ((sum < givenSum) && (startCurent < n))    // Compair
  36.                 {
  37.                     sum += array[startCurent]; // Calc the sum
  38.                     if (sum == givenSum)
  39.                     {
  40.                         startPosition = i;
  41.                         sumLength = countLength;
  42.                     }
  43.                     startCurent++;
  44.                     countLength++;
  45.                 }
  46.             }
  47.             else if (array[i] == givenSum)
  48.             {
  49.                 same++;
  50.             }
  51.         }
  52.  
  53.         // Print
  54.         while (sumLength >= 0)
  55.         {
  56.             Console.Write(array[startPosition] + " ");
  57.             startPosition++;
  58.             sumLength--;
  59.         }
  60.         Console.WriteLine("And " + same + " same number as a GIVEN SUM!");
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement