BorislavBorisov

Редици.02.Сума образувана от поредни елементи

Oct 14th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. class LongestEqualSubsequence
  3. {
  4.     static int[] array = { 1, 23, 3, 4, 6, 5, 5, 6, 8, 6, 3, 7, 8, 45 };
  5.     static void Main()
  6.     {
  7.         int sum = 24;
  8.        
  9.         int newSum = 0, index = 0;
  10.         bool isFound = false;
  11.  
  12.         for (int i = 0; i < array.Length; i++)
  13.         {
  14.             newSum = array[i];
  15.             for (int j = i + 1; j < array.Length; j++)
  16.             {
  17.                 newSum += array[j];
  18.                 if (newSum == sum)
  19.                 {
  20.                     isFound = true;
  21.                     index = i;
  22.                     PrintNumbers(index,sum);
  23.                     break;
  24.                 }
  25.             }
  26.             newSum = 0;
  27.         }
  28.         if (!isFound)
  29.         {
  30.             Console.WriteLine("Nqma takova jivotno");
  31.         }
  32.      }
  33.  
  34.     static void PrintNumbers(int index, int sum)
  35.     {
  36.         int result = 0;
  37.         while(result < sum)
  38.         {
  39.             Console.Write(array[index] + " ");
  40.             result += array[index];
  41.             index++;
  42.         }
  43.         Console.Write(" = " + sum);
  44.         Console.WriteLine();
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment