Advertisement
xellscream

SumOfS

Jan 13th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2.  
  3. class SumOfS
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Enter value for S: ");
  8.         int s = int.Parse(Console.ReadLine());
  9.  
  10.         int[] arr = { 4, 3, 1, 4, 2, 5, 8 };
  11.         int lenght = arr.Length, needed = 0, count, counted = 0, current = 0, sum;
  12.  
  13.         for (int index = 0; index < lenght; index++)
  14.         {
  15.             sum = arr[index];
  16.             count = 0;
  17.  
  18.             for (int i = index; i < lenght; i++)
  19.             {
  20.                 if (i < lenght - 1)
  21.                 {
  22.                     sum += arr[i + 1];
  23.                     if (sum > s)
  24.                     {
  25.                         break;
  26.                     }
  27.                     else if (sum == s)
  28.                     {
  29.                         needed = sum;
  30.                         counted = count;
  31.                         current = index;
  32.                     }
  33.                     else
  34.                     {
  35.                         count++;
  36.                         continue;
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     break;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         Console.Write("{");
  47.         for (int j = 0; j < lenght; j++)
  48.         {
  49.             Console.Write(arr[j]);
  50.             if (j < lenght - 1)
  51.             {
  52.                 Console.Write(", ");
  53.             }
  54.             else
  55.             {
  56.                 continue;
  57.             }
  58.         }
  59.         Console.Write("} > ");
  60.         if (needed == 0)
  61.         {
  62.             Console.WriteLine("There is no sequence with sum of S in the array.");
  63.         }
  64.         else
  65.         {
  66.             Console.Write("S={0}, ", s);
  67.  
  68.             for (int i = current, j = counted + 1; j >= 0; j--, i++)
  69.             {
  70.                 Console.Write(arr[i]);
  71.                 if (j > 0)
  72.                 {
  73.                     Console.Write(", ");
  74.                 }
  75.             }
  76.             Console.WriteLine();
  77.         }
  78.     {
  79.          
  80.     }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement