Yanlış sorular veya ben aptal mıyım?

2 Cevap

Biraz programlama görev içerir Paul Jungwirth tarafından codinghorror.com üzerine bir blog sonrası açıklama var:

Bunu sırayla, sayılar 123456789 var. Elde edilen ifade 2.001 eşittir, böylece her sayı arasında, sen, hiçbir şey, bir artı işareti, ya da bir çarpma işareti ya eklemeniz gerekir. Tüm çözümler yazdıran bir program yazın. (İki vardır.)

Fore, ben gitmek var diye düşündüm, ama 2001 için bir sonuç alabilirsiniz eğer ben mahvolurum. Aşağıda kod ses olduğunu düşünüyorum ve ben 2001 yılında neden sıfır çözümler olduğunu hesaplıyorlar. Göre benim kodu, 2002 yılı için iki çözüm vardır. Haklı mıyım yoksa ben yanlış mıyım?

/**
 * 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";
    }
}

2 Cevap

Size daha fazla bağlantılı aynı sayfada aşağı .... =)

"Paul Jungwirth yazdı:

You have the numbers 123456789, in that order. Between each number, you must insert either nothing, a plus sign, or a multiplication sign, so that the resulting expression equals 2001. Write a program that prints all solutions. (There are two.)

Ben 2002 değil, 2001 kastetti. :)

(Just correcting for anyone else like me who obsessively tries to solve little "practice" problems like this one, and then hit Google when their result doesn't match the stated answer. ;) Damn, some of those Perl examples are ugly.)"

Sayı 2002'dir.

Recursive çözüm JavaScript onbir hatları (ancak bu muhtemelen bu belirli bir senaryo için kendi rulo kod başka bir on ya da öylesine çizgiler alacağını, standart bir JavaScript işlevi olan dize ifadesi değerlendirme hariç) alır:

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');