DonJabone

HomeWorkArray18

Jul 2nd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. class Array18                  
  6. {
  7.    
  8.     static void Main(string[] args)
  9.     {
  10.         Console.Write("Insert how long will be your array : ");
  11.         int n = int.Parse(Console.ReadLine());
  12.         int[] MyArray = new int[n];
  13.         for (int i = 0; i < MyArray.Length; i++)
  14.         {
  15.             Console.Write("{0} : ", i + 1);
  16.             MyArray[i] = int.Parse(Console.ReadLine());
  17.         }
  18.         int MaxCombination = (int)Math.Pow(2, MyArray.Length) - 1;
  19.         int maxSubset = 0;
  20.         string stringMaxSubset = "";
  21.         for (int i = 1; i <= MaxCombination; i++)
  22.         {
  23.             List<int> TempList = new List<int>();
  24.             int count = 0;
  25.             for (int j = 0; j < MyArray.Length; j++)
  26.             {
  27.  
  28.                 int mask = 1 << j;
  29.                 int nAndMask = i & mask;
  30.                 int bit = nAndMask >> j;
  31.                 if (bit == 1)
  32.                 {
  33.                     if (count == 0)
  34.                     {
  35.                         TempList.Add(MyArray[j]);
  36.                         count++;
  37.                         continue;
  38.                     }
  39.                     if (TempList[TempList.Count - 1] <= MyArray[j])
  40.                     {
  41.                         TempList.Add(MyArray[j]);
  42.                         count++;
  43.                     }
  44.                     else
  45.                     {
  46.                         break;
  47.                     }
  48.                 }
  49.                 if (count > maxSubset)
  50.                 {
  51.                     maxSubset = count;
  52.                     stringMaxSubset = "";
  53.                     for (int h = 0; h < TempList.Count; h++)
  54.                     {
  55.                         stringMaxSubset += TempList[h] + " ";
  56.                     }
  57.                 }
  58.  
  59.             }
  60.         }
  61.         Console.WriteLine("Longest subset in increasing order is {0}" , stringMaxSubset);
  62.  
  63.     }
  64.    
  65. }
Advertisement
Add Comment
Please, Sign In to add comment