PHP复数选择

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

我写了下面的代码:

  1. $row_array = range( 1 , $puzzle_size , 1 );
  2. $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的值没有给我所需要的输出。它现在给我的是:

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

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

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

例二:

  1. Array
  2. (
  3. [0] => 1
  4. [1] => 4
  5. [2] => 7
  6. )

例三:

  1. Array
  2. (
  3. [0] => 2
  4. [1] => 4
  5. [2] => 7
  6. )

我不知道该怎么做。

pexxcrt2

pexxcrt21#

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

  1. <?php
  2. // Total number of items required
  3. $puzzle_size = 50;
  4. // Total items to be displayed
  5. $total_numbers_to_display = 5;
  6. // Seed the array items as per $puzzle_size value
  7. $row_array = range( 1 , $puzzle_size , 1 );
  8. // 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.
  9. function randItems($sourceArray){
  10. // declare global variable
  11. global $total_numbers_to_display;
  12. // generate random array elements and return
  13. return array_rand( $sourceArray , $total_numbers_to_display );
  14. }
  15. // Function to check whether any consequtive numbers are generated from randItems function above. If exists, then re-pick the random elements from $row_array
  16. function get_non_consequtive_numbers($outputArray)
  17. {
  18. // declare global variable
  19. global $row_array;
  20. // iterate through the array provided as parameter
  21. foreach($outputArray as $num){
  22. // check if any previous or next number exists in the generated array. If exists, then re-pick the random elements from $row_array
  23. if( in_array(($num+1) , $outputArray ) or in_array(($num-1), $outputArray ) )
  24. {
  25. // calling randItems and recursive function to check.
  26. return get_non_consequtive_numbers(randItems($row_array));
  27. }
  28. }
  29. // if no consequtive numbers found, then the array is valid and return.
  30. return $outputArray;
  31. }
  32. // initiating the generation process
  33. $yes_array = get_non_consequtive_numbers(randItems($row_array));
  34. // printing the output obtained from above line
  35. print_r($yes_array);

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

展开查看全部
c9qzyr3d

c9qzyr3d2#

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

  1. function get_non_consequtive_numbers( $puzzle_size , $total_numbers_to_display ) {
  2. $non_consequtive_numbers = [ ];
  3. $eligible_numbers = range( 1 , $puzzle_size , 1 );
  4. while ( count( $non_consequtive_numbers ) < $total_numbers_to_display ) {
  5. # get a random number
  6. $candidate_number = rand( 1 , $puzzle_size );
  7. # check if number is in $eligible_numbers
  8. if ( in_array( $candidate_number , $eligible_numbers ) ) {
  9. # add to $non_consequtive_numvbers
  10. $non_consequtive_numbers[ count( $non_consequtive_numbers ) + 1 ] = $candidate_number;
  11. # remove from $eligible_numbers
  12. foreach ( range( ( $candidate_number - 1 ) , ( $candidate_number + 1 ) , 1 ) AS $ineligible_number ) {
  13. if ( ( $key = array_search( $ineligible_number , $eligible_numbers ) ) !== false ) {
  14. unset( $eligible_numbers[$key] );
  15. }
  16. }
  17. }
  18. }
  19. return $non_consequtive_numbers;
  20. }
展开查看全部

相关问题