PHP-如何获得“所有两位数字”,它们的加法给出一个特定数字

时间:2019-02-18 23:32:40

标签: php loops

我创建了一个小脑力游戏,需要猜出一个数字从1到18的两个数字组合,例如:

1: 10 (1+0 = 1)
2: 11 (1+1 = 2)
3: 12 (1+2 = 3), 21 (2+1 = 3)
4: 22 (2+2 = 4), 31 (3+1= 4), 13 (1+3 = 4)
.
.
.
.
.
.

如何获取每个数字以18结尾的数字?

4 个答案:

答案 0 :(得分:1)

for($target = 1; $target < 19; $target++) {
    echo "$target: ";
    for($i = 10; $i<100; $i++) {
        $parts = str_split((string) $i);
        if (array_sum($parts) == $target) {
            echo "$i ";
        }
    }

    echo "\n";
}

$target是我们当前正在寻找的总和。内部循环使用标准的PHP函数str_split()和array_sum()来检查所有总和目标的所有2位数字,以分解数字并计算各部分的总和。

输出:

1: 10 
2: 11 20 
3: 12 21 30 
4: 13 22 31 40 
5: 14 23 32 41 50 
6: 15 24 33 42 51 60 
7: 16 25 34 43 52 61 70 
8: 17 26 35 44 53 62 71 80 
9: 18 27 36 45 54 63 72 81 90 
10: 19 28 37 46 55 64 73 82 91 
11: 29 38 47 56 65 74 83 92 
12: 39 48 57 66 75 84 93 
13: 49 58 67 76 85 94 
14: 59 68 77 86 95 
15: 69 78 87 96 
16: 79 88 97 
17: 89 98 
18: 99

答案 1 :(得分:0)

如果您只是想创建一个游戏,允许用户输入一个数字,该数字成为其数字的总和,并将其与计算机生成的数字进行比较

<?php
function sum($carry, $num){
  $carry += $num;
  return $carry;
}
function game($computer, $guess){
  if($computer === array_reduce(str_split($guess), 'sum')){
    return 'win';
  }
  return 'lose';
}
// force computer to choose 9
$computer = 9; $guess = 18;
echo game($computer, $guess);
// random 1-18 computer choice - will show win when computer picks 4
$computer = rand(1, 18); $guess = 22;
echo game($computer, $guess);
?>

您也可以这样做:

<?php
class GuessingGame{
  public $computerFunc;
  public function __construct($computerFunc = null){
    $this->computerFunc = $computerFunc;
  }
  public function guess($guess){
    $user = array_reduce(str_split($guess), function($carry, $num){
      $carry += $num;
      return $carry;
    });
    if(call_user_func($this->computerFunc) === $user){
      return true;
    }
    return false;
  }
}
function computerFunc(){
  return rand(1, 18);
}
$game = new GuessingGame('computerFunc');
$test = $game->guess(22) ? 'win' : 'lose';
function guarantee(){
  return 9;
}
$game->computerFunc = 'guarantee';
$test2 = $game->guess(18) ? 'win' : 'lose';
$test .= ' ----- '.$test2 ;
echo $test;
?>

答案 2 :(得分:0)

要创建此类集合,您可以使用嵌套的for循环,每个循环从0到最大值进行迭代,在这种情况下为9,因为您希望所有产生的加数对都为两位数。

for ($x = 0; $x <= 9; $x++) {
    for ($y = 0; $y <= 9; $y++) {
        $result[$x + $y][] = $x . $y;
    }
}

然后输出每组加数对。

foreach ($result as $sum => $addends) {
    echo "$sum: " . implode(' ', $addends) . "\n";
}

答案 3 :(得分:0)

使用多个循环会浪费处理能力,因为它将执行不必要/无用的迭代。对于每个游戏,您将确定一个随机数供玩家“继续练习”。使用带有简单减法的单循环来确定所有正确答案。如果您每次都要生成此查找数组,请不要使用蛮力。 使用一个循环和数学逻辑。 (实际上,您可以/应该对整个查找数组进行硬编码,而不会造成任何明显的内存丢失;我认为这是一个学术/个人编码难题。)

代码:(Demo

$random_target = rand(1,18);
echo "Find all two digit numbers whose individual digits add up to: " , $random_target , "\n\n";

for ($x = 1; $x < 10; ++$x) {  // the tens digit
    if (($diff = $random_target - $x) >= 0 && $diff < 10 ) {  // if ones digit is only "1 digit" it qualifies
        $result[] = "$x$diff";
    }
}

var_export($result);

可能的输出:

查找所有两位数字,其单个数字加起来为:13

array (
  0 => '49',
  1 => '58',
  2 => '67',
  3 => '76',
  4 => '85',
  5 => '94',
)

p.s。如果要构建完整的查找数组,请使用@ Don'tPanic的精益技术(不浪费迭代)。然后将结果硬编码到脚本中-无需再次生成正确的结果。

for ($x = 1; $x <= 9; ++$x) {
    for ($y = 0; $y <= 9; ++$y) {
        $result[$x + $y][] = "$x$y";
    }
}
var_export($result);