Advertisement
Guest User

Untitled

a guest
Dec 20th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System;
  2. //Write a program that finds in given array of integers a sequence of given sum S (if present). Example: {4, 3, 1, 4, 2, 5, 8}, S=11  {4, 2, 5}
  3.  
  4. class SequenceOfGivenSum
  5. {
  6.     static void Main()
  7.     {
  8.         int sumS = int.Parse(Console.ReadLine());
  9.         int length = int.Parse(Console.ReadLine());
  10.         int[] arr = new int[length];
  11.  
  12.         for (int i = 0; i < arr.Length; i++)
  13.         {
  14.             arr[i] = int.Parse(Console.ReadLine());
  15.         }
  16.  
  17.         int elementPositionBegin = 0;
  18.         int elementPositionEnd = 0;
  19.         int temp = 0;
  20.  
  21.         for (int i = 0; i < arr.Length - 1; i++)
  22.         {
  23.             temp = arr[i];
  24.  
  25.             for (int j = i; j < arr.Length - 1; j++)
  26.             {
  27.                 if (temp + arr[j+1] == sumS)
  28.                 {
  29.                     elementPositionBegin = i;
  30.                     elementPositionEnd = j + 1;                                        
  31.                 }
  32.                 else
  33.                 {
  34.                     temp += arr[j + 1];
  35.                 }
  36.             }
  37.         }
  38.  
  39.         for (int i = elementPositionBegin; i <= elementPositionEnd; i++)
  40.         {
  41.             Console.Write("{0} ", arr[elementPositionBegin]);
  42.             elementPositionBegin++;
  43.         }      
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement