Venciity

[C#] BOOK Arrays - 6 Задача

Jan 6th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. /*Напишете програма, която намира максималната поредица от нарастващи елементи в масив arr[n].
  2.  * Елементите може и да не са последователни. Пример: {9, 6, 2, 7, 4, 7, 6, 5, 8, 4} --> {2, 4, 6, 8}.*/
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _6_Exercise
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("Enter array length: ");
  17.             int m = int.Parse(Console.ReadLine());
  18.             int[] array = new int[m];
  19.  
  20.             //enter array elements
  21.             for (int i = 0; i < array.Length; i++)
  22.             {
  23.                 Console.Write("Number {0} = ", i + 1);
  24.                 array[i] = int.Parse(Console.ReadLine());
  25.             }
  26.  
  27.             List<int> maxList = new List<int>();
  28.             for (int mask = 1; mask < (1 << array.Length); mask++)
  29.             {
  30.                 List<int> list = new List<int>();
  31.                 for (int i = 0; i < array.Length; i++)
  32.                 {
  33.                     if (((mask >> i) & 1) == 1)
  34.                     {
  35.                         if (list.Count == 0 || list[list.Count - 1] < array[i])
  36.                             list.Add(array[i]);
  37.                     }
  38.                     if (list.Count > maxList.Count)
  39.                     {
  40.                         maxList = list;
  41.                     }
  42.  
  43.                 }
  44.             }
  45.             Console.WriteLine();
  46.             Console.Write("maximum subset by increasing elements : ");
  47.             foreach (int element in maxList)
  48.             {
  49.                 Console.Write(element + " ");
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment