How to make Triangle Roll-Up with PHP?

后端 未结 4 386
借酒劲吻你
借酒劲吻你 2020-12-07 04:53

I found a question on google like this :

When given the input : 4, 7, 3, 6, 7

The output like this :

81

40 41

21 19 22

11 10 9 13

4  7          


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 05:07

     0) {
            foreach ($input as $k=>$v) {
                if (isset($input[$k+1]))
                    $line[] = $v + $input[$k+1];
            }
            $return = implode(' ', $input);
            rollup($line);
        }
        if (!empty($return))
            echo $return . '
    '; } ?>

提交回复
热议问题