任何人都可以告诉我如何找到给定数组中的最高,第二高数字,最低第二最低数字
var numbers = new[] {855,3,64,6,24,75,3,6,24,45};
任何指针和建议都会有所帮助。感谢
答案 0 :(得分:4)
您没有指定复杂性要求:一种方法是按降序对数组进行排序,然后选择顶部,第二和第三项。
另一种方法是构建一个Heap,然后执行删除root 3次(每次删除后重建堆)。
答案 1 :(得分:4)
假设您在阵列中至少有2个项目,则可以使用 OrderBy()
和 ElementAt()
:
var numbers = new[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
var secondLowest = numbers.OrderBy(num => num).ElementAt(1);
var secondHighest = numbers.OrderBy(num => num).Reverse().ElementAt(1);
获得最高和最低版本更简单,可以使用 Max()
和 Min()
LINQ方法完成。
var lowest = numbers.Min();
var highest = numbers.Max();
如果您担心复杂性,可以使用 Selection algorithm 获得更好的结果。使用它,您可以执行 O(n) 复杂性中的操作。
答案 2 :(得分:4)
你也可以试试这个 -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class secondHighestLowest : System.Web.UI.Page
{
int[] arr = new int[10] { 45, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
protected void Page_Load(object sender, EventArgs e)
{
secondHighestLowestNumber();
secoundLowestNumber();
}
private void secondHighestLowestNumber()
{
int firstHighestNumber = arr[0];
int secondHighestNumber = arr[0];
for(int i = 0; i<arr.Length; i++)
{
if (arr[i]>firstHighestNumber)
{
firstHighestNumber = arr[i];
}
}
for (int x = 0; x < arr.Length; x++)
{
if (arr[x]>secondHighestNumber && firstHighestNumber!=arr[x])
{
secondHighestNumber = arr[x];
}
}
Response.Write("secondHighestNumber---- " + secondHighestNumber + "</br>");
}
private void secoundLowestNumber()
{
int firstLowestNumber = arr[0];
int secondLowestNumber = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < firstLowestNumber)
{
firstLowestNumber = arr[i];
}
}
for (int x = 0; x < arr.Length; x++)
{
if (arr[x] < secondLowestNumber && firstLowestNumber != arr[x])
{
secondLowestNumber = arr[x];
}
}
Response.Write("secondLowestNumber---- " + secondLowestNumber + "</br>");
}
}
希望这有用:)
答案 3 :(得分:4)
使用Linq概念
var a = new int[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
var Max = a.Max(z => z);
var Min = a.Min( z => z);
var SMax = a.OrderByDescending(z=>z).Skip(1).First();
var SMin = a.OrderBy(z => z).Skip(1).First();
答案 4 :(得分:0)
int[] i = new int[] { 4, 8, 1, 9, 2, 7, 3 };
Array.Sort(i);
Console.WriteLine("Highest number :" + i[i.Length-1]);
Console.WriteLine("Second highest number :"+i[i.Length-2]);
Console.WriteLine("Lowest number :" + i[i.Length-i.Length]);
Console.WriteLine("Second Lowest number :" + i[i.Length -i.Length+1]);
Output : Highest number : 9
Second highest number : 8
Lowest number : 1
Second Lowest number : 2
答案 5 :(得分:0)
为何可以在
中完成两个循环 int[] myArray = new int[] { 2, 4, 3, 6, 9 };
int max1 = 0;
int max2 = 0;
for (int i = 0; i < myArray.Length; i++)
{
if (myArray[i] > max1)
{
max2 = max1;
max1 = myArray[i];
}
else
{
max2 = myArray[i];
}
}
Console.WriteLine("first" + max1.ToString());
Console.WriteLine("Second" + max2.ToString());
Console.ReadKey();
答案 6 :(得分:0)
我首先使用选择排序算法按升序排列它们,然后我显示值。
--- d = Derived1() ----
d.foo(0) -> foo_from_bar(5)
d.bar(0) -> 5
d.baz(0) -> baz_from_foo(foo_from_bar(5))
--- d = Derived2() ----
d.foo(0) -> 6
d.bar(0) -> bar_from_baz(baz_from_foo(6))
d.baz(0) -> baz_from_foo(6)
--- d = Derived3() ----
d.foo(0) -> foo_from_bar(bar_from_baz(7))
d.bar(0) -> bar_from_baz(7)
d.baz(0) -> 7
答案 7 :(得分:0)
即使有重复记录,此算法也适用。
int[] intArrayInput= new int[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45, 855 };
使用排序:
Array.Sort(intArrayInput);
int intMax = intArrayInput[intInput.Length - 1];
int intLow = intArrayInput[0];
使用LINQ:
int intSecondMax = intArrayInput.OrderByDescending(x => x).Distinct().Skip(1).First();
int intSecondLow = intArrayInput.OrderBy(x => x).Distinct().Skip(1).First();
答案 8 :(得分:0)
Array.Sort(new);
string newNumber = string.Join(" ", new[0]);
return newNumber;
Array.Sort(new);
string newNumber = string.Join(" ", new[1]);
return newNumber;
Array.Sort(new);
string newNumber = string.Join(" ", new[new.Length - 1]);
return newNumber;
Array.Sort(new);
string newNumber = string.Join(" ", new[new.Length - 1]);
return newNumber;
答案 9 :(得分:0)
使用常规方法找出最大,第二大,最小和第二最小数的最短方法:
Start-Process python -ArgumentList "test.py" -RedirectStandardOutput out.log -RedirectStandardError err.log -NoNewWindow -Wait
答案 10 :(得分:-1)
int[] myUnSortArray = { 1, 5, 8, 3, 10, 6, 19, 5, 4, 4 };
int[] SortedArray = (from number in myUnSortArray
orderby number ascending
select number).ToArray();
int highestValue = SortedArray.Max();
int SecondHighest = SortedArray.Last(m => m < highestValue);