数组中最长的子序列

时间:2013-01-13 07:54:57

标签: c#

我正在尝试使用List解决我的任务,我知道我非常接近解决它,但我现在被困住了。代码中有些东西不行,我无法弄清楚它是什么。你能看一看并帮忙:

        /*
Write a program that reads an array of integers and removes from it a minimal number of elements in such way that the
remaining array is sorted in increasing order. Print the remaining sorted array.
Example: {6, 1, 4, 3, 0, 3, 6, 4, 5}  {1, 3, 3, 4, 5}
 */
using System;
using System.Collections.Generic;

class RemoveMinimalElements
{

    static void Main()
    {
        int n;
        n = int.Parse(Console.ReadLine());
        List<int> arr = new List<int>();
        List<int> sorted = new List<int>();
        int maxSubsetLenght = 0;

        for (int i = 0; i < n; i++)
        {
            arr.Add(int.Parse(Console.ReadLine()));
        }


        for (int i = 1; i <= (int)Math.Pow(2, n) - 1; i++)
        {
            int tempSubsetLenght = 0;
            string tempString = "";
            List<int> temp = new List<int>();


            for (int j = 1; j <= n; j++)
            {
                int andMask = i & (1 << j);
                int bit = andMask >> j;

                if (bit == 1)
                {
                    temp.Add(arr[n - 1 - j]);
                    tempSubsetLenght++;
                }
                if (tempSubsetLenght > maxSubsetLenght)
                {
                    maxSubsetLenght = tempSubsetLenght;

                    for(int k =1; k < temp.Count; k ++)
                    {
                        if (temp[k] >= temp[k - 1])
                        {
                            sorted = temp;
                        }
                    }
                }
            }
        }

        for (int i = sorted.Count - 1; i > 0; i--)
        {
            Console.WriteLine(sorted[i]);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

我没有按照代码,我只是测试了你的应用程序。

这是我的第一个输入:5。

然后我输入了这5个输入2,4,6,8,10所以

arr = {2,4,6,8,10};

当它到达最后一行时,它给了我 ArguementOutOfRangeException (Index was out of range. Must be non-negative and less than the size of the collection.)因为它试图获取arr [item]并且item为6所以它试图获取{{1哪个不存在。

答案 1 :(得分:1)

我不知道详尽的搜索是否适合您的情况,但这对您有用吗?

static void Main(string[] args)
        {
            int[] input = new[] { 6, 1, 4, 3, 0, 3, 6, 4, 5 };
            int[] expectedOutput = new[] { 1, 3, 3, 4, 5 };

            int[] solution = TryGetSolution(input);

            Console.WriteLine("Input: " + FormatNumbers(input));
            Console.WriteLine("Expected Output: " + FormatNumbers(expectedOutput));
            Console.WriteLine("Output: " + FormatNumbers(solution));
            Console.ReadLine();
        }

        private static string FormatNumbers(int[] numbers)
        {
            return string.Join(", ", numbers);
        }

        private static int[] TryGetSolution(int[] input)
        {
            return TryWithoutAnyItem(input);
        }

        private static int[] TryWithoutAnyItem(int[] items)
        {
            return Enumerable.Range(0, items.Length)
                             .Select(i => TryWithoutItem(items, i))
                             .Where(solution => solution != null)
                             .OrderByDescending(solution => solution.Length)
                             .FirstOrDefault();
        }

        private static int[] TryWithoutItem(int[] items, int withoutIndex)
        {
            if (IsSorted(items)) return items;
            var removed = items.Take(withoutIndex).Concat(items.Skip(withoutIndex + 1));
            return TryWithoutAnyItem(removed.ToArray());
        }

        private static bool IsSorted(IEnumerable<int> items)
        {
            return items.Zip(items.Skip(1), (a, b) => a.CompareTo(b)).All(c => c <= 0);
        }
    }

答案 2 :(得分:0)

我解决了!非常感谢您的支持。我是一个初学者,我无法使用和理解更难的东西,所以这就是我对已经知道的事情所做的:

/*
Write a program that reads an array of integers and removes from it a minimal number of elements in such way that the
remaining array is sorted in increasing order. Print the remaining sorted array.
Example: {6, 1, 4, 3, 0, 3, 6, 4, 5}  {1, 3, 3, 4, 5}
 */
using System;
using System.Collections.Generic;

class RemoveMinimalElements
{
    static bool CheckAscending(List<int> list)
    {
        bool ascending = true;

        for (int i = 0; i < list.Count - 1; i++)
        {
            if (list[i] > list[i + 1])
            {
                ascending = false;
            }
        }

        return ascending;
    }

    static void Main()
    {
        int n;
        n = int.Parse(Console.ReadLine());
        List<int> arr = new List<int>();
        List<int> sorted = new List<int>();
        int maxSubsetLenght = 0;

        for (int i = 0; i < n; i++)
        {
            arr.Add(int.Parse(Console.ReadLine()));
        }


        for (int i = 1; i <= (int)Math.Pow(2, n) - 1; i++)
        {
            int tempSubsetLenght = 0;
            List<int> temp = new List<int>();


            for (int j = 1; j <= n; j++)
            {
                if (((i >> (j - 1)) & 1) == 1)
                {
                    temp.Add(arr[j - 1]);
                    tempSubsetLenght++;
                }

            }

            if ((tempSubsetLenght > maxSubsetLenght) && (CheckAscending(temp)))
            {
                sorted = temp;
                maxSubsetLenght = tempSubsetLenght;
            }
        }

        for (int i = 0; i < sorted.Count; i++)
        {
            Console.WriteLine(sorted[i]);
        }
    }
}

答案 3 :(得分:0)

这对我有用

   private static void FindLongestRisingSequence(int[] inputArray)
    {
        int[] array = inputArray;
         
        List<int> list = new List<int>();
        List<int> longestList = new List<int>();
        int highestCount = 1;
        for (int i = 0; i < array.Length; i++)
        {
            list.Add(array[i]);
            for (int j = i+1; j < array.Length; j++)
            {
                if (array[i] < array[j])
                {
                    list.Add(array[j]);
                    i++;
                }
                else 
                {
                    break;
                }
                i = j;
            }
            // Compare with in previous lists  
            if (highestCount < list.Count)
            {
                highestCount = list.Count;
                longestList = new List<int>(list);
            }
            list.Clear();

        }
        Console.WriteLine();

        // Print list  
        Console.WriteLine("The longest subsequence");
        foreach (int iterator in longestList)
        {
            Console.Write(iterator + " ");
        }
        Console.WriteLine();
    }