2017-05-05 8 views

답변

1

아니라,이 내 걸릴 :

  1. 은 원래 배열의 복사본을 만듭니다;
  2. 복사 된 배열을 정렬하여 가장 높은 n 개의 숫자를 찾습니다.
  3. 원래 배열을 살펴보고 이전 단계에서 n 개의 가장 높은 숫자와 숫자를 비교 한 결과 배열에 필요한 숫자를 이동합니다.

var a = [12.3,15.4,1,13.3,16.5], n = 3, x = 0, c =[]; // c - the resulting array 
 
var b = a.slice(); // copy the original array to sort it 
 
for(var i = 1; i < b.length; i++) { // insertion sorting of the copy 
 
    var temp = b[i]; 
 
    for(var j = i - 1; j >= 0 && temp > b[j]; j--) b[j + 1] = b[j]; 
 
    b[j + 1] = temp; 
 
} 
 
for(var i = 0; i < a.length; i++) { // creating the resulting array 
 
    for(var j = 0; j < n; j++) { 
 
    if(a[i] === b[j]) { 
 
     c[x] = a[i]; x++; // or just c.push(a[i]); 
 
    } 
 
    } 
 
} 
 
console.log(c);

예는 자바 스크립트로 작성 다소 간단하지만, 사실, 그것은 아주 언어 무관하며 일을한다.