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

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

PHP中文网
Release: 2016-05-25 17:07:03
Original
1106 people have browsed it

[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);
Copy after login

                   

                   

Related labels:
php
Statement of this Website
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
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template