2010-03-04 92 views
9

有上codinghorror.com保罗Jungwirth,其中包括一个小的编程任务博客中评论:错误地问我还是我愚蠢?

你有号码123456789,按照这个顺序。在每个数字之间,您必须插入无,加号或乘号,以便得到的表达式等于2001.编写一个打印所有解决方案的程序。 (有两个。)

无聊,我想,我会去的,但如果我能得到2001年的结果,我会被诅咒。我认为下面的代码是健全的,我认为根据我的代码,2002年有两种解决方案。我是对的还是我错了?

/** 
* Take the numbers 123456789 and form expressions by inserting one of '' 
* (empty string), '+' or '*' between each number. 
* Find (2) solutions such that the expression evaluates to the number 2001 
*/ 

$input = array(1,2,3,4,5,6,7,8,9); 

// an array of strings representing 8 digit, base 3 numbers 
$ops = array(); 
$numOps = sizeof($input)-1; // always 8 
$mask = str_repeat('0', $numOps); // mask of 8 zeros for padding 

// generate the ops array 
$limit = pow(3, $numOps) -1; 
for ($i = 0; $i <= $limit; $i++) { 
    $s = (string) $i; 
    $s = base_convert($s, 10, 3); 
    $ops[] = substr($mask, 0, $numOps - strlen($s)) . $s; 
} 

// for each element in the ops array, generate an expression by inserting 
// '', '*' or '+' between the numbers in $input. e.g. element 11111111 will 
// result in 1+2+3+4+5+6+7+8+9 
$limit = sizeof($ops); 
$stringResult = null; 
$numericResult = null; 
for ($i = 0; $i < $limit; $i++) { 
    $l = $numOps; 
    $stringResult = ''; 
    $numericResult = 0; 
    for ($j = 0; $j <= $l; $j++) { 
     $stringResult .= (string) $input[$j]; 
     switch (substr($ops[$i], $j, 1)) { 
      case '0': 
       break; 
      case '1': 
       $stringResult .= '+'; 
       break; 
      case '2': 
       $stringResult .= '*'; 
       break; 
      default : 
     } 
    } 

    // evaluate the expression 

    // split the expression into smaller ones to be added together 
    $temp = explode('+', $stringResult); 
    $additionElems = array(); 
    foreach ($temp as $subExpressions) 
    { 
     // split each of those into ones to be multiplied together 
     $multplicationElems = explode('*', $subExpressions); 
     $working = 1; 
     foreach ($multplicationElems as $operand) { 
      $working *= $operand; 
     } 
     $additionElems[] = $working; 
    } 
    $numericResult = 0; 
    foreach($additionElems as $operand) 
    { 
     $numericResult += $operand; 
    } 

    if ($numericResult == 2001) { 
     echo "{$stringResult}\n"; 
    } 
} 

回答

12

再往下您链接到....在同一页=)

“保罗Jungwirth写道:

你有号123456789,在 的顺序每个数字之间。 ,你 必须插入任何东西,加号 符号或乘号,所以 ,得到的表达式等于 2001.编写一个打印所有解决方案的程序(有两个。)

我想你的意思是2002年,2001年没有:)

(只是修正为别人像 我谁痴迷试图解决这样一个 小 “实践”的问题,然后打谷歌时,他们的 结果与指定的 答案不符。 ;)该死,其中的一些的Perl 实例是难看)”

+3

而且Jungwirth证实了他的意思是2002年提出一些看法下来从 – 2010-03-04 23:22:40

+0

这真棒 – 2010-03-04 23:24:56

+2

球很好,至少我得到了一些代码审查的做法! – jah 2010-03-04 23:26:08

3

数量是2002年

递归溶液需要11行的JavaScript(不包括字符串表达式求值,这是一个标准的JavaScript功能,但是它可能需要再过十年左右行的代码推出自己的这种特定情况下):!

function combine (digit,exp) {      
    if (digit > 9) {        
     if (eval(exp) == 2002) alert(exp+'=2002'); 
     return;         
    }            
    combine(digit+1,exp+'+'+digit);    
    combine(digit+1,exp+'*'+digit);    
    combine(digit+1,exp+digit);     
    return;          
}             
combine(2,'1');