그래서 정렬되지 않은 숫자 배열이 int[] anArray = { 1, 5, 2, 7 };있고 배열 에서 가장 큰 값인 7과 3의 값과 인덱스를 모두 가져와야합니다. 어떻게해야합니까?
그래서 정렬되지 않은 숫자 배열이 int[] anArray = { 1, 5, 2, 7 };있고 배열 에서 가장 큰 값인 7과 3의 값과 인덱스를 모두 가져와야합니다. 어떻게해야합니까?
답변:
이것은 가장 매력적인 방법은 아니지만 작동합니다.
(필수 using System.Linq;)
int maxValue = anArray.Max();
int maxIndex = anArray.ToList().IndexOf(maxValue);
.ToList(), 배열이 명시 적으로 구현할 필요도 없습니다IList
IList인터페이스를 구현 하지만 명시 적으로 구현합니다 : msdn.microsoft.com/en-us/library/… . (배열은 해당하는 일반 IList<T>인터페이스 도 구현합니다 .)
ToList()항상 복사하는 것입니다. 메서드를 가끔 복사하고 때로는 복사하지 않는 것은 끔찍한 생각 일 것입니다. 이것은 꽤 미친 앨리어싱 버그로 이어질 것입니다. 실제로 구현은 ToList()다소return new List(source)
인덱스가 정렬되지 않은 경우 배열을 한 번 이상 반복하여 가장 높은 값을 찾아야합니다. 간단한 for루프를 사용합니다 .
int? maxVal = null; //nullable so this works even if you have all super-low negatives
int index = -1;
for (int i = 0; i < anArray.Length; i++)
{
int thisNum = anArray[i];
if (!maxVal.HasValue || thisNum > maxVal.Value)
{
maxVal = thisNum;
index = i;
}
}
이것은 LINQ 또는 다른 단선 솔루션을 사용하는 것보다 더 장황하지만 아마도 조금 더 빠를 것입니다. O (N)보다 빠르게 만들 수있는 방법은 없습니다.
maxVal인덱스 0 (배열 길이가 1 이상이라고 가정)의 배열 값 으로 초기화 하고 0 으로 초기화 하고 index에서 for 루프를 시작하여 반복 하나를 저장할 수 i = 1있습니다.
필수 LINQ one [1] -liner :
var max = anArray.Select((value, index) => new {value, index})
.OrderByDescending(vi => vi.value)
.First();
(정렬은 아마도 다른 솔루션에 비해 성능 저하 일 것입니다.)
[1] : 주어진 값 "1"에 대해.
간결한 한 줄 :
var max = anArray.Select((n, i) => (Number: n, Index: i)).Max();
테스트 케이스 :
var anArray = new int[] { 1, 5, 2, 7 };
var max = anArray.Select((n, i) => (Number: n, Index: i)).Max();
Console.WriteLine($"Maximum number = {max.Number}, on index {max.Index}.");
// Maximum number = 7, on index 4.
풍모:
비고 :
anArray.Select((n, i) => ( Index: i, Number: n)).Max()튜플을 비교하는 방식으로 인해 최대 수 대신 최대 인덱스 를 찾습니다 (item1이 가장 중요 함 등)
두 가지 접근 방식이 있습니다. 배열이 비어있을 때 처리를 추가 할 수 있습니다.
public static void FindMax()
{
// Advantages:
// * Functional approach
// * Compact code
// Cons:
// * We are indexing into the array twice at each step
// * The Range and IEnumerable add a bit of overhead
// * Many people will find this code harder to understand
int[] array = { 1, 5, 2, 7 };
int maxIndex = Enumerable.Range(0, array.Length).Aggregate((max, i) => array[max] > array[i] ? max : i);
int maxInt = array[maxIndex];
Console.WriteLine($"Maximum int {maxInt} is found at index {maxIndex}");
}
public static void FindMax2()
{
// Advantages:
// * Near-optimal performance
int[] array = { 1, 5, 2, 7 };
int maxIndex = -1;
int maxInt = Int32.MinValue;
// Modern C# compilers optimize the case where we put array.Length in the condition
for (int i = 0; i < array.Length; i++)
{
int value = array[i];
if (value > maxInt)
{
maxInt = value;
maxIndex = i;
}
}
Console.WriteLine($"Maximum int {maxInt} is found at index {maxIndex}");
}
벨로우즈 코드에 대한 출력 :
00 : 00 : 00.3279270-max1 00 : 00 : 00.2615935-max2 00 : 00 : 00.6010360-max3 (arr.Max ())
배열의 100000000 int는 그다지 큰 차이는 아니지만 여전히 ...
class Program
{
static void Main(string[] args)
{
int[] arr = new int[100000000];
Random randNum = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = randNum.Next(-100000000, 100000000);
}
Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2 = new Stopwatch();
Stopwatch stopwatch3 = new Stopwatch();
stopwatch1.Start();
var max = GetMaxFullIterate(arr);
Debug.WriteLine( stopwatch1.Elapsed.ToString());
stopwatch2.Start();
var max2 = GetMaxPartialIterate(arr);
Debug.WriteLine( stopwatch2.Elapsed.ToString());
stopwatch3.Start();
var max3 = arr.Max();
Debug.WriteLine(stopwatch3.Elapsed.ToString());
}
private static int GetMaxPartialIterate(int[] arr)
{
var max = arr[0];
var idx = 0;
for (int i = arr.Length / 2; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[idx] > max)
{
max = arr[idx];
}
idx++;
}
return max;
}
private static int GetMaxFullIterate(int[] arr)
{
var max = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
public static class ArrayExtensions
{
public static int MaxIndexOf<T>(this T[] input)
{
var max = input.Max();
int index = Array.IndexOf(input, max);
return index;
}
}
이것은 모든 변수 유형에서 작동합니다 ...
var array = new int[]{1, 2, 4, 10, 0, 2};
var index = array.MaxIndexOf();
var array = new double[]{1.0, 2.0, 4.0, 10.0, 0.0, 2.0};
var index = array.MaxIndexOf();
public static void Main()
{
int a,b=0;
int []arr={1, 2, 2, 3, 3, 4, 5, 6, 5, 7, 7, 7, 100, 8, 1};
for(int i=arr.Length-1 ; i>-1 ; i--)
{
a = arr[i];
if(a > b)
{
b=a;
}
}
Console.WriteLine(b);
}
다음은 괜찮은 상수 인자를 가진 O (n) 인 LINQ 솔루션입니다.
int[] anArray = { 1, 5, 2, 7, 1 };
int index = 0;
int maxIndex = 0;
var max = anArray.Aggregate(
(oldMax, element) => {
++index;
if (element <= oldMax)
return oldMax;
maxIndex = index;
return element;
}
);
Console.WriteLine("max = {0}, maxIndex = {1}", max, maxIndex);
그러나 for성능에 관심이 있다면 명시 적 lop을 작성해야 합니다.
그냥 다른 관점 사용 DataTable. 및 DataTable이라는 2 개의 열로 a 를 선언하십시오 . 옵션과 및 값 을 모두 열에 추가하십시오 . 그런 다음 루프를 사용하고 각 배열 항목을 에 행 으로 삽입합니다 . 그런 다음 방법 을 사용 하여 최대 값이있는 행을 선택합니다.indexvalAutoIncrementAutoIncrementSeedAutoIncrementStep1indexforeachdatatableSelect
암호
int[] anArray = { 1, 5, 2, 7 };
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("index"), new DataColumn("val")});
dt.Columns["index"].AutoIncrement = true;
dt.Columns["index"].AutoIncrementSeed = 1;
dt.Columns["index"].AutoIncrementStep = 1;
foreach(int i in anArray)
dt.Rows.Add(null, i);
DataRow[] dr = dt.Select("[val] = MAX([val])");
Console.WriteLine("Max Value = {0}, Index = {1}", dr[0][1], dr[0][0]);
산출
Max Value = 7, Index = 4
배열에서 가장 큰 숫자와 가장 작은 숫자를 찾습니다.
int[] arr = new int[] {35,28,20,89,63,45,12};
int big = 0;
int little = 0;
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
if (arr[i] > arr[0])
{
big = arr[i];
}
else
{
little = arr[i];
}
}
Console.WriteLine("most big number inside of array is " + big);
Console.WriteLine("most little number inside of array is " + little);
다음을 고려하십시오.
/// <summary>
/// Returns max value
/// </summary>
/// <param name="arr">array to search in</param>
/// <param name="index">index of the max value</param>
/// <returns>max value</returns>
public static int MaxAt(int[] arr, out int index)
{
index = -1;
int max = Int32.MinValue;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
index = i;
}
}
return max;
}
용법:
int m, at;
m = MaxAt(new int[]{1,2,7,3,4,5,6}, out at);
Console.WriteLine("Max: {0}, found at: {1}", m, at);
이것은 for우리가 골프로 향하고 있다면 몸이없는 루프로 할 수 있습니다 ;)
//a is the array
int mi = a.Length - 1;
for (int i=-1; ++i<a.Length-1; mi=a[mi]<a[i]?i:mi) ;
++i<a.Length-1검사는 마지막 인덱스 검사 를 생략합니다. 최대 인덱스가 시작하는 마지막 인덱스 인 것처럼 설정하면 신경 쓰지 않습니다. 루프가 다른 요소에 대해 실행되면 완료되고 하나 또는 다른 것이 참입니다.
mimi, 우리는 초기에 붙어mi실제 작업은 포스트 루프 수정 자에 의해 수행됩니다.
a[mi]예 : 인덱스 배열 mi)이 현재 항목보다 작습니까?
mi을 기억 하여 새 항목을 저장 하십시오 i.mi(no-op)작업이 끝나면 최대 값을 찾을 인덱스가 있습니다. 논리적으로 최대 값은a[mi]
배열이 있고 최대 값의 인덱스, 최대 값의 실제 값을 알고 있다면 "최대 값 찾기 및 최대 값 찾기"가 최대 값을 추적하는 데 얼마나 필요한지 알 수 없습니다. 인덱스를 사용하여 배열을 인덱싱하는 사소한 경우입니다.