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 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

4 回答 4

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 所以它试图获取arr[6]不存在的。

于 2013-01-13T08:19:27.497 回答
0

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

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);
        }
    }
于 2013-01-13T11:19:56.663 回答
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]);
        }
    }
}
于 2013-01-13T15:58:45.870 回答
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();
    }
于 2020-10-04T01:32:24.750 回答