2016-07-01 2 views
0

이 같은 소수 값을 갖는 배열이 함께 그룹으로 배열 :분할 최소값 및 분 요소

$a = array(1.66, 28.13, 3.37, 2, 12, 88.90, 6.88, 0.57, 1.50); 

I는 최소 합계 값이 최소 5 개 요소 그룹으로이 배열을 분할 할 수 어떻게 50라고 말하게하십시오!

이 기능을 사용했지만 제대로 수행하지 못했습니다.

function split_into_groups($input_array) { 

    $limit = 50; 
    rsort($input_array); 
    $b = array(array()); 
    $index = 0; 
    foreach($input_array as $i){ 
     if($i + array_sum($b[$index]) > $limit){ 
      $b[++$index] = array(); 
     } 
     $b[$index][] = $i; 
    } 
    return $b; 
} 

출력 :

array(4) { 
    [0]=> 
    array(0) { 
    } 
    [1]=> 
    array(1) { 
    [0]=> 
    float(88.9) 
    } 
    [2]=> 
    array(3) { 
    [0]=> 
    float(28.13) 
    [1]=> 
    int(12) 
    [2]=> 
    float(6.88) 
    } 
    [3]=> 
    array(5) { 
    [0]=> 
    float(3.37) 
    [1]=> 
    int(2) 
    [2]=> 
    float(1.66) 
    [3]=> 
    float(1.5) 
    [4]=> 
    float(0.57) 
    } 
} 
+0

당신이 시도한 것과 당신이 갇혀있는 곳을 보여주십시오. –

+0

루프 스루, 합계 금액의 집계를 유지할 때 선택한 합계가 5 –

+0

합계를 출력하십시오. –

답변

1

이것은 약간 까다 롭습니다. 그러나 이것이 당신이 겪은 일이되기를 바랍니다. 코드의 메모가 충분히 설명력이 있어야합니다. 그렇지 않은 경우 메모를 남기셔야합니다.

<?php 
$a = array(1.66, 28.13, 3.37, 2, 12, 88.90, 6.88, 0.57, 1.50); 
// split into groups of at least 5 elements, 
// each group must have sum of at least 50 

// create array to hold arrays of groups: 
$groups = array(0); 

// initialize a counter to say which element of $groups to add to: 
$group_selector = 0; 

// this must be declared as an array, creating a multidimentional array: 
$groups[$group_selector] = array(); 

// loop through each item in $a, deciding where to put it: 
for ($i=0; $i < count($a); $i++) { 

    // check if there is less than 5 elements in current group, OR the sum is less than 50: 
    if ((count($groups[$group_selector]) < 5) || (array_sum($groups[$group_selector]) < 50)) { 

     // add current $a item to current group: 
     array_push($groups[$group_selector], $a[$i]); 

    } else { 
     // increment the group selector and declare it as a new array: 
     $group_selector++; 
     $groups[$group_selector] = array(); 
     array_push($groups[$group_selector], $a[$i]); 
    } 

} # end of main for loop 

// print the raw array (doesn't look very good): 
print_r($groups); 

echo '<br><br>'; 

// print the sum of each group, and the number of items it contains: 
for ($i=0; $i < count($groups); $i++) { 
    echo 'count: ' . array_sum($groups[$i]) . '<br>number of items: ' . count($groups[$i]) . '<br><br>'; 
} 
?> 
0

작은 배열의 경우, 무차별 접근 방식은 잘 합리적으로 작동 할 수 있습니다.

이 알고리즘은 모든 제약 조건을 충족 할 때까지 간단하게 배열을 뒤섞거나 최대 반복 횟수 후에 중지합니다.

define('MAX_ITER',  10000); 
define('MIN_CHUNK_SUM', 50); 
define('MAX_CHUNK_SIZE', 5); 

$chunk = solve(
    array(1.66, 28.13, 3.37, 2, 12, 88.90, 6.88, 0.57, 1.50), 
    $i 
); 

if($chunk === false) { 
    printf("No solution found\n"); 
} 
else { 
    printf("Found a solution in %d iterations\n", $i); 

    foreach($chunk as $n => $c) { 
    arsort($c); 
    printf("Chunk #%d: SUM = %g with { %s }\n", $n, array_sum($c), implode(", ", $c)); 
    } 
} 

function solve($a, &$i) { 
    for($i = 0; $i < MAX_ITER; $i++) { 
    shuffle($a); 
    $chunk = array_chunk($a, MAX_CHUNK_SIZE); 

    foreach($chunk as $c) { 
     if(array_sum($c) < MIN_CHUNK_SUM) { 
     continue 2; 
     } 
    } 
    return $chunk; 
    } 
    return false; 
} 

예시 출력 : arsort()을 표시하기 전에, 용액을 표준화보다 다른 목적을 제공 없다고

// Found a solution in 4 iterations 
// Chunk #0: SUM = 50.51 with { 28.13, 12, 6.88, 2, 1.5 } 
// Chunk #1: SUM = 94.5 with { 88.9, 3.37, 1.66, 0.57 } 

참고.