看见很简单,就自己写了个很笨的方法。 int[] arr = { 1,3,2,5,7,1,2,3,4 }; List<int> list = new List<int>(); List<int> list2 = new List<int>(); int index = 0; list.Add(arr[0]); for (int i = 1; i < arr.Length; i++) { if (arr[i] > arr[i - 1]) { list.Add(arr[i]); } else { if (list2.Count < list.Count) { list2 = new List<int>(list); list = new List<int>(); list.Add(arr[i]); index = i - list2.Count; } } } if (list2.Count < list.Count) { list2 = new List<int>(list); index = arr.Length - list2.Count; } Console.WriteLine("最长递增数组的从{0}开始,为:",index); for (int j = 0; j < list2.Count; j++) { Console.WriteLine(list2[j]); } Console.ReadKey();
简单写了一下,仅供参考: static void MaxIncreaseArray(int[] intArray) { int max = 1; int index = 0; int temp = 1; for (int i = 1; i < intArray.Length; i++) { if (intArray[i] >= intArray[i - 1]) { temp++; } else { if (temp > max) { max = temp; index = i - 1; } temp = 1; } if (i == intArray.Length - 1) { if (temp > max) { max = temp; index = i; } } } for (int i = max -1 ; i >= 0; i--) { Console.WriteLine(intArray[index - i]); } } }
private void btnFind_Click(object sender, EventArgs e) { string[] listString = this.txtList.Text.Split(','); int[] list=new int[listString.Length]; for(int i=0;i<listString.Length;i++) { list[i]=System.Convert.ToInt32(listString[i]); } int maxLength = 1; string str = string.Empty; for(int ii =0; ii<list.Length-1;ii++) { int length = 1; int temp=list[ii]; int index = ii; StringBuilder sb = new StringBuilder(); sb.Append(temp.ToString() + ","); while (index<list.Length-1 && temp + 1 == list[index+1]) { temp = list[index+1]; index += 1; length += 1; sb.Append(temp.ToString()+","); } if (length >= maxLength) { maxLength = length; length = 1; str = sb.ToString().TrimEnd(','); sb = new StringBuilder(); } } this.txtSubList.Text = maxLength.ToString()+":"+str; }
List<int> list = new List<int>();
List<int> list2 = new List<int>();
int index = 0;
list.Add(arr[0]);
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > arr[i - 1])
{
list.Add(arr[i]);
}
else
{
if (list2.Count < list.Count)
{
list2 = new List<int>(list);
list = new List<int>();
list.Add(arr[i]);
index = i - list2.Count;
}
}
}
if (list2.Count < list.Count)
{
list2 = new List<int>(list);
index = arr.Length - list2.Count;
} Console.WriteLine("最长递增数组的从{0}开始,为:",index);
for (int j = 0; j < list2.Count; j++)
{
Console.WriteLine(list2[j]);
} Console.ReadKey();
static void MaxIncreaseArray(int[] intArray)
{
int max = 1;
int index = 0;
int temp = 1; for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] >= intArray[i - 1])
{
temp++;
}
else
{
if (temp > max)
{
max = temp;
index = i - 1;
} temp = 1;
} if (i == intArray.Length - 1)
{
if (temp > max)
{
max = temp;
index = i;
}
}
} for (int i = max -1 ; i >= 0; i--)
{
Console.WriteLine(intArray[index - i]);
}
}
}
{
string[] listString = this.txtList.Text.Split(',');
int[] list=new int[listString.Length];
for(int i=0;i<listString.Length;i++)
{
list[i]=System.Convert.ToInt32(listString[i]);
}
int maxLength = 1;
string str = string.Empty; for(int ii =0; ii<list.Length-1;ii++)
{
int length = 1;
int temp=list[ii];
int index = ii;
StringBuilder sb = new StringBuilder();
sb.Append(temp.ToString() + ",");
while (index<list.Length-1 && temp + 1 == list[index+1])
{
temp = list[index+1];
index += 1;
length += 1;
sb.Append(temp.ToString()+",");
}
if (length >= maxLength)
{
maxLength = length;
length = 1;
str = sb.ToString().TrimEnd(',');
sb = new StringBuilder();
}
} this.txtSubList.Text = maxLength.ToString()+":"+str;
}