일부 C# 도움말이 필요합니다. I 5 개 값 정수 배열있다 : I의 값을 정렬 한 다음 세 가지 값 (10 조합 가능성)과의 조합에 대한 최대 값을 찾을 필요배열 서브 세트의 최대 값 찾기
Int[] arrayValues = { 10, 8, 6, 5, 3 };
가되도록 최대 총합이 3 개 값 마지막 3 자리에 있습니다.
일부 C# 도움말이 필요합니다. I 5 개 값 정수 배열있다 : I의 값을 정렬 한 다음 세 가지 값 (10 조합 가능성)과의 조합에 대한 최대 값을 찾을 필요배열 서브 세트의 최대 값 찾기
Int[] arrayValues = { 10, 8, 6, 5, 3 };
가되도록 최대 총합이 3 개 값 마지막 3 자리에 있습니다.
알고리즘은 다음
코드는 그냥 질문 같은 요구 사항을 게시하는 것보다 더 많은 일을하기 위해 필요한이 같은 (이 최적화 될 수있다),
int[] orginalArray = { 10, 8, 6, 5, 3 };
int[] copyArray = new int[orginalArray.Length];
int[] resultArray = new int[orginalArray.Length];
// Make a copy of the orginal array
Array.Copy(orginalArray,0, copyArray, 0,orginalArray.Length);
// Sort the copied array in ascendng order (last 3 elements are the largest 3 elements)
Array.Sort(copyArray);
// Array to store the largest elements
int[] largest = new int[3];
for (int i = copyArray.Length - 3, j = 0; i < copyArray.Length; i++, j++)
{
largest[j] = copyArray[i];
}
// Sum of the largest elements
int largestSum = largest.Sum();
// Copy the non largest elements to the result array (in the original order)
for (int i = 0, j=0; i < orginalArray.Length; i++)
{
if (!largest.Contains(orginalArray[i]))
{
resultArray[j++] = orginalArray[i];
}
}
// Copy the largest elements to the last 3 positions
for(int i=largest.Length - 1, j=0;i<resultArray.Length;i++, j++)
{
resultArray[i] = largest[j];
}
// Result - resultArray[] : 5 3 6 8 10
// Largest sum combination - largest[]: 6 8 10
// Sum of largest combination - largestSum: 24
단지 오름차순으로 정렬 된 배열입니다.
arrayValues.sort()
작동 및 오름차순으로 번호를 기재한다
입니다. 시도한 것들 중 몇 가지를 알려주십시오. 이 질문에 대해 생각해 보았고 그것을 해결하는 최선의 방법에 대한 몇 가지 증거를 제시하십시오. 가급적이면 제대로 작동하지 않는 코드 샘플을 보여주십시오. 하지만 가장 중요한 것은 * 실제 질문 *입니다. –
숙제? (15 문자 ...) –
가 발견되었습니다. 10은 최대입니다. –