给定一个未经排序的整数数组,找到最长且连续的递增序列
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 6, 4, 5, 6, 7, 2};
int[] b = GetMaxContinuousArray(a);
Console.WriteLine("最长且连续的的递增序列长度为 :{0}", b[0]);
string c = "";
for (int i = b[1] - b[0] + 1; i <= b[1]; i++)
if (i == b[1])
c = c + a[i];
else
c = c + a[i] + ",";
Console.WriteLine("最长且连续的的递增序列为 :{0}", c);
}
//返回递增序列长度 和 该序列的末位索引
public static int[] GetMaxContinuousArray(int[] Array)
{
int[] a = {0,0};
if (Array.Length == 0)
return a;
int maxLeng = 1;
int count = 0;
int index = 0;
for (int i = 0; i < Array.Length - 1; i++)
{
if (Array[i] < Array[i + 1])
{
maxLeng++;
index = i + 1;
}
else
{
if (maxLeng > count)
count = maxLeng;
maxLeng = 1;
}
}
if (maxLeng > count)
a[0] = maxLeng ;
else
a[0] = count;
a[1] = index;
return a;
}