Home  >  Article  >  php教程  >  两个蛋判断最低从哪一楼扔下来刚好会碎

两个蛋判断最低从哪一楼扔下来刚好会碎

PHP中文网
PHP中文网Original
2016-05-25 17:07:031033browse

[PHP]代码  

<?php

function twoEggProblem($hight){
    return ceil((sqrt(8*$hight+1)-1)/2);
}
echo twoEggProblem(100);

好吧,我改改:test是一台蛋碎机,0表示不碎,1表示。。。
function twoEggProblem($hight){
    $len = ceil((sqrt(8*$hight+1)-1)/2);
    $lenForFor = $len+1; //这个变量名不错吧、、、、
    $rs = 0;
    for ($i=1;$i<=$len;$i++){
        $_len = $lenForFor-$i;
        $rs += $_len;
        if (test($rs) == 0) continue;
        $rs -= $_len;
        for ($j = 1;$j<$_len;$j++) {
           $rs += 1;
           if (test($rs) == 1) return $rs;
        }
        return ++$rs;
    }
    return 0;//oh ... ... 这是一只坚强的当当。。。
}
echo twoEggProblem(100);

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn