Venciity

[Jumpstart C#] Arrays 06B-TheSumOfANumberOfElements

Feb 18th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. /*  Нека е дадена редица от цели числа.
  2.     - при зададено от потребителя S, отпечатайте подредиците, сумата на елементите на които е равна на S
  3.     Например: { 4, 3, 1, 4, 2, 5, 8 }, S=11 -> { 4, 2, 5 }   */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _06B_TheSumOfANumberOfElements
  11. {
  12.     class TheSumOfANumberOfElements
  13.     {
  14.         static void Main()
  15.         {
  16.             Console.Write("Numbers of array = ");
  17.             int n = Int32.Parse(Console.ReadLine());
  18.             int[] array = new int[n];
  19.             for (int i = 0; i < n; i++)
  20.             {
  21.                 Console.Write("Element {0} = ", i + 1);
  22.                 array[i] = Int32.Parse(Console.ReadLine());
  23.             }
  24.  
  25.             int arrayLen = array.Length;
  26.  
  27.             Console.Write("Sum = ");
  28.             int s = Int32.Parse(Console.ReadLine());
  29.  
  30.  
  31.             for (int i = 0; i < arrayLen; i++)
  32.             {
  33.                 int sum = 0;
  34.  
  35.                 for (int j = i; j < arrayLen; j++)
  36.                 {
  37.                     sum += array[j];
  38.  
  39.                     if (s == sum)
  40.                     {
  41.                         for (int k = i; k <= j; k++)
  42.                         {
  43.                             Console.Write(array[k] + " ");
  44.                         }
  45.                         Console.WriteLine();
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment