Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class GivenSumOfSSeq
- {
- static void Main()
- {
- Console.Write("Please, Insert the length of the array: ");
- int n = int.Parse(Console.ReadLine());
- Console.Write("Now the SUM which we find!: ");
- int givenSum = int.Parse(Console.ReadLine());
- // Read the array
- int[] array = new int[n];
- for (int i = 0; i < n; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- // Check
- int sum = 0;
- int startCurent = 0;
- int startPosition = 0;
- int sumLength = 0;
- int same = 0;
- int countLength = 0;
- for (int i = 0; i < n; i++) // Get the start position
- {
- if (array[i] < givenSum)
- {
- startCurent = i;
- sum = 0;
- countLength = 0;
- while ((sum < givenSum) && (startCurent < n)) // Compair
- {
- sum += array[startCurent]; // Calc the sum
- if (sum == givenSum)
- {
- startPosition = i;
- sumLength = countLength;
- }
- startCurent++;
- countLength++;
- }
- }
- else if (array[i] == givenSum)
- {
- same++;
- }
- }
- // Print
- while (sumLength >= 0)
- {
- Console.Write(array[startPosition] + " ");
- startPosition++;
- sumLength--;
- }
- Console.WriteLine("And " + same + " same number as a GIVEN SUM!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement