我必须编写一个程序,该程序在数组arr [n]中找到递增元素的最大序列。元素不必连续放置。例如:{9,6,2,7,4,7,6,5,8,4}à{2,4,6,8}。下面是我的代码,它没有解决任务。你能帮我吗?
using System;
namespace MaxIncreasingSequence
{
class MaxIncreasingSequence
{
public static void Main(string[] args)
{
//Console.WriteLine("No of elements in array: ");
//int n = int.Parse(Console.ReadLine());
//arr[]
int n = 10;
int[] arr = new int[n];
//read from the console each element of the array
for (int i = 0; i < n; i++)
{
Console.Write("Array[{0}] = ", i);
arr[i] = int.Parse(Console.ReadLine());
}
//len[]
int[] len = new int[n];
for (int i = 0; i < n; i++)
{
len[i] = 1;
}
//correct len[]
for (int indexCount = 0; indexCount < n; indexCount++)
{
for (int indexNumber = 0; indexNumber < indexCount; indexNumber++)
{
if (arr[indexCount] > arr[indexNumber] && len[indexCount] < len[indexCount] + 1)
{
len[indexCount] = len[indexNumber] + 1;
}
}
}
//print the new len[]
//just to keep track on numebers
Console.WriteLine();
Console.Write("{");
for (int i = 0; i < n; i++)
{
Console.Write(" " + len[i] + " ");
}
Console.WriteLine("}");
//search for the max number in len[]
int max = int.MinValue;
int maxPosition = 0;
for (int i = 0; i < len.Length; i++)
{
if (len[i] > max)
{
max = len[i];
maxPosition = i;
}
}
//create the result in result[]
int[] result = new int[max];
int j = (max - 1);
int maxCount = max;
for (int k = maxPosition; k > 0; k--)
{
if (len[k] == max)
{
result[j] = arr[k];
max--;
j--;
}
}
//preint the result[]
Console.WriteLine();
Console.Write("{");
for (int i = 0; i < maxCount; i++)
{
Console.Write(" " + result[i] + " ");
}
Console.WriteLine("}");
}
}
}