计算题--不要二

题目描述

二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子座标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
<?php

$handler = fopen('php://stdin','r');
$arr = explode(' ',fgets($handler));
function cal($w,$h) {
    //发现规律,四行就一个周期
    //第1、2行蛋糕的个数跟列关系为:(h/4)*2 + h%4>2?2:h%4
    //第3、4行蛋糕的个数跟列关系为:(h/4)*2 + h%4<2?0:h%4-2
    //多出的行要加上
    $s = floor($w/4) * ((floor($h/4)*2 + ($h%4>2 ? 2 : $h%4) + floor($h/4)*2 + ($h%4<2 ? 0 : $h%4-2))) * 2;
    if ($w%4 > 0) $s += floor($h/4)*2 + ($h%4>2 ? 2 : $h%4);
    if ($w%4 > 1) $s += floor($h/4)*2 + ($h%4>2 ? 2 : $h%4);
    if ($w%4 > 2) $s += floor($h/4)*2 + ($h%4<2 ? 0 : ($h%4-2));
    echo $s;
}
cal($arr[0],$arr[1]);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章