andreykata

ArraysExercise10

Jan 14th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2.     class Program
  3.     {
  4.         static void Main(string[] args)
  5.         {
  6.             int[] array = { 4, 3, 1, 4, 2, 5, 8 };
  7.             int s = 15;
  8.             int sum = array[0];            
  9.             int start = 0, count = 1;
  10.             for (int i = 1; i < array.Length; i++)
  11.             {
  12.                 if (sum + array[i] == s)
  13.                 {
  14.                     count++;
  15.                     for (int j = start; j < start + count; j++)
  16.                     {
  17.                         Console.Write("{0} ",array[j]);
  18.                     }
  19.                     Console.WriteLine();
  20.                 }
  21.                 else if (sum + array[i] < s)
  22.                 {
  23.                     sum = array[i] + sum;
  24.                     count++;                    
  25.                 }
  26.                 else if (sum + array[i] > s)
  27.                 {
  28.                     sum = array[i - count + 1];
  29.                     start = i - count + 1;
  30.                     i = start;
  31.                     count = 1;
  32.                    
  33.                 }
  34.             }
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment