Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SumOfS
- {
- static void Main()
- {
- Console.Write("Enter value for S: ");
- int s = int.Parse(Console.ReadLine());
- int[] arr = { 4, 3, 1, 4, 2, 5, 8 };
- int lenght = arr.Length, needed = 0, count, counted = 0, current = 0, sum;
- for (int index = 0; index < lenght; index++)
- {
- sum = arr[index];
- count = 0;
- for (int i = index; i < lenght; i++)
- {
- if (i < lenght - 1)
- {
- sum += arr[i + 1];
- if (sum > s)
- {
- break;
- }
- else if (sum == s)
- {
- needed = sum;
- counted = count;
- current = index;
- }
- else
- {
- count++;
- continue;
- }
- }
- else
- {
- break;
- }
- }
- }
- Console.Write("{");
- for (int j = 0; j < lenght; j++)
- {
- Console.Write(arr[j]);
- if (j < lenght - 1)
- {
- Console.Write(", ");
- }
- else
- {
- continue;
- }
- }
- Console.Write("} > ");
- if (needed == 0)
- {
- Console.WriteLine("There is no sequence with sum of S in the array.");
- }
- else
- {
- Console.Write("S={0}, ", s);
- for (int i = current, j = counted + 1; j >= 0; j--, i++)
- {
- Console.Write(arr[i]);
- if (j > 0)
- {
- Console.Write(", ");
- }
- }
- Console.WriteLine();
- }
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement