【问题标题】:Longest Ascending Sequence C#最长升序 C#
【发布时间】:2020-01-07 14:53:29
【问题描述】:

所以我对 C# 还很陌生,遇到了一个问题,需要我:

在整数数组中搜索最长的整数升序序列。如果对于所有 i (1 ≤ i ≤ n - 1),如果 xi

到目前为止,这是我的代码(我只能按升序对数组进行排序):

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace AscendingSequences
{
    class AscendingSequences
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Ascending Sequence!");
            GenerateNumber();
        }

        public static void GenerateNumber()
        {
            int i, j, n, number;
            int[] array = new int[100];
            int[] array1 = new int[100];
            Random random = new Random();

            Console.Write("\nInput the number of element to be store in the array: ");
            n = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nThe {0} array is generating-----\n", n);
            for (i = 0; i < n; i++)
            {
                array[i] = random.Next(1, 20);
                Console.Write("\nThe array|{0}| is {1} ", i, array[i]);
            }
            for(i=0; i<n; i++)
            {
                for(j=i+1; j<n; j++)
                {
                    if(array[j] < array[i])
                    {
                        number = array[i];
                        array[i] = array[j];
                        array[j] = number;
                    }
                }
            }
            Console.Write("\nElements of array in sorted ascending order is: ");
            for(i=0; i<n;i++)
            {
                Console.Write("{0} ", array[i]);
            }
        }
    }
}

这是给我的任务:

【问题讨论】:

  • 你没有提供你的问题是什么?
  • @fubo 也许是因为排序数组已经被问过很多次了,一次又一次地回答它很烦人。
  • @fubo 因为阅读了 HimBromBeere 的评论 ????
  • 据我所知,这个问题并不是重复的。问题不在于如何排序序列,而在于找到有序序列
  • @jdweng 嗯嗯,我想是的。我从我的教授那里得到这个练习。即使是提出问题的我也不完全理解我的教授想要什么,所以我必须发布整个问题+我到目前为止的进展,以便有人可以给我一些启发:(

标签: c#


【解决方案1】:

您对数组进行第一次排序的方法是错误的,不幸的是,这导致一些人感到困惑。

如果您从排列数组开始,您将丢失这些重要元素的原始位置信息。相反,您应该遍历您的数组并检查当前元素是否大于前一个元素(升序)。

    //start and length are the "current" values and max are the max found
    int start = 0, length = 0, maxstart = 0, maxlength = 0;
    //loop through array (starting from index 1 to avoid out of bounds)
    for (int i = 1; i < array.Length; i++)
    {
        //check if current sequence is longer than previously recorded
        if (length > maxlength)
        {
            maxstart = start;
            maxlength = length;
        }

        //if previous element <= to current element
        if (array[i - 1] <= array[i])
        {
            //if the current element isn't part of the current sequence, then start a new sequence
            if (start + length < i)
            {
                start = i - 1;
                length = 2;
            }
            else
            {
                //count the length
                length++;
            }
        }
    }

这是一个带有工作代码的 .net fiddle: https://dotnetfiddle.net/1GLmEB

编辑: 在 cmets 中回复您关于其工作原理的问题start + length &lt; i

此条件检查当前值是否是序列的一部分。 变量start 是最后/当前找到的序列的开始,length 是长度。 当此条件返回 true 时,表示它位于最后找到的序列之外,它会重置 startlength(true = outside, false = inside)的值

让我们通过一些案例来看看为什么会这样:

1 2 3 1 1 2 3 1 1
      * > > > e ^
start = 3 (*)
length = 4 (>)
i = 8 (^)
3+4 = e

3+4<8 //true : new sequence

所以最后找到的序列从 3 开始,长度为 4。 这意味着该序列将在索引 7 处结束。 由于我们目前正在循环中检查索引 8,我们可以看到它不是同一序列的一部分。

1 2 3 1 1 2 3 4 1
      * > > > > ê
start = 3 (*)
length = 5 (>)
i = 8 (^)
3+4 = e

3+5<8 //false : current sequence

所以最后找到的序列从 3 开始,长度为 5。 这意味着该序列将在索引 8 处结束。 由于我们当前正在循环中检查索引 8,我们可以看到它同一序列的一部分。

事后看来,如果翻转这个 if 语句(真 = 内部,假 = 外部),可能不会那么混乱。不过,我现在不会更改代码以避免进一步混淆。

【讨论】:

  • 感谢您的回答!!我完全被给定的问题弄糊涂了,所以我在解决方案中犯了一个错误^^
  • @Steve 我知道你的意图是好的,谢谢,但我在这个问题上已经很困惑 2 天了。这不是家庭作业,而是可供学生更多练习的可选练习。我不会将答案中的所有内容都复制到我的程序中。在动手之前我会先尝试理解它:)
  • @Steve,如果一个人想学习,那么她\他可以从一个正确的答案中学到很多东西(更不用说她\他会知道 dotnetfiddle 的存在)。如果她\他只是在完成作业后,你不能教你做什么。
  • @confused_unga_bunga 如果我的回答让你感到困惑,请告诉我,我会尽力解释。
  • @JoostK 没关系,我现在明白了。感谢您到目前为止的帮助!
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多