PHP复数选择

jgzswidk  于 2023-02-28  发布在  PHP
关注(0)|答案(2)|浏览(128)

我写了下面的代码:

$row_array = range( 1 , $puzzle_size , 1 );
$yes_array = array_rand( $row_array , $total_numbers_to_display );

$puzzle_size和$total_numbers_to_display的值取决于难度级别:
$puzzle_size将为8、20和40,用于简单、中等和困难级别。$total_numbers_to_display = 3
$yes_array的值没有给我所需要的输出。它现在给我的是:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

我所要求的是每个结果之间至少有1个数字差距。
例一:

Array
(
    [0] => 1
    [1] => 3
    [2] => 5
)

例二:

Array
(
    [0] => 1
    [1] => 4
    [2] => 7
)

例三:

Array
(
    [0] => 2
    [1] => 4
    [2] => 7
)

我不知道该怎么做。

pexxcrt2

pexxcrt21#

请按如下所示编辑您的代码:

<?php 
// Total number of items required
$puzzle_size = 50;

// Total items to be displayed 
$total_numbers_to_display = 5;

// Seed the array items as per $puzzle_size value
$row_array = range( 1 , $puzzle_size , 1 );

// Function to get the random array items and pick only the number of items required. array_rand() will return only the keys of the array, not the values.
function randItems($sourceArray){
    
    // declare global variable
    global $total_numbers_to_display;

    // generate random array elements and return 
    return array_rand( $sourceArray , $total_numbers_to_display );
}

// Function to check whether any consequtive numbers are generated from randItems function above. If exists, then re-pick the random elements from $row_array 
function get_non_consequtive_numbers($outputArray)
{
    // declare global variable
    global $row_array;

   // iterate through the array provided as parameter
   foreach($outputArray as $num){

        // check if any previous or next number exists in the generated array. If exists, then re-pick the random elements from $row_array
        if( in_array(($num+1) , $outputArray ) or in_array(($num-1), $outputArray ) )
        {

            // calling randItems and recursive function to check.
            return get_non_consequtive_numbers(randItems($row_array));   
        }
    }
    
    // if no consequtive numbers found, then the array is valid and return.
    return $outputArray;      
}

// initiating the generation process
$yes_array = get_non_consequtive_numbers(randItems($row_array));

// printing the output obtained from above line
print_r($yes_array);

输出与问题中的预期一致。

c9qzyr3d

c9qzyr3d2#

@PracticalOpportunist的答案让我走上了正确的轨道,这是我写的函数:

function get_non_consequtive_numbers( $puzzle_size , $total_numbers_to_display ) {

    $non_consequtive_numbers = [ ];

    $eligible_numbers = range( 1 , $puzzle_size , 1 );

    while ( count( $non_consequtive_numbers ) < $total_numbers_to_display ) {

        # get a random number

            $candidate_number = rand( 1 , $puzzle_size );

        # check if number is in $eligible_numbers

            if ( in_array( $candidate_number , $eligible_numbers ) ) {

                # add to $non_consequtive_numvbers

                    $non_consequtive_numbers[ count( $non_consequtive_numbers ) + 1 ] = $candidate_number;

                # remove from $eligible_numbers

                    foreach ( range( ( $candidate_number - 1 ) , ( $candidate_number + 1 ) , 1 ) AS $ineligible_number ) {

                        if ( ( $key = array_search( $ineligible_number , $eligible_numbers ) ) !== false ) {

                            unset( $eligible_numbers[$key] );

                        }

                    }

            }

    }

    return $non_consequtive_numbers;

}

相关问题