Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Напишете програма, която намира максималната поредица от нарастващи елементи в масив arr[n].
- * Елементите може и да не са последователни. Пример: {9, 6, 2, 7, 4, 7, 6, 5, 8, 4} --> {2, 4, 6, 8}.*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _6_Exercise
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Enter array length: ");
- int m = int.Parse(Console.ReadLine());
- int[] array = new int[m];
- //enter array elements
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write("Number {0} = ", i + 1);
- array[i] = int.Parse(Console.ReadLine());
- }
- List<int> maxList = new List<int>();
- for (int mask = 1; mask < (1 << array.Length); mask++)
- {
- List<int> list = new List<int>();
- for (int i = 0; i < array.Length; i++)
- {
- if (((mask >> i) & 1) == 1)
- {
- if (list.Count == 0 || list[list.Count - 1] < array[i])
- list.Add(array[i]);
- }
- if (list.Count > maxList.Count)
- {
- maxList = list;
- }
- }
- }
- Console.WriteLine();
- Console.Write("maximum subset by increasing elements : ");
- foreach (int element in maxList)
- {
- Console.Write(element + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment