格子の中に存在する長方形の数を答える問題。
[java]
public class RectangularGrid {
public long countRectangles(int width, int height) {
long wid = (long)width;
long hei = (long)height;
long allRect = (wid+1)*wid/2 * (hei+1)*hei/2;
long square = 0L;
for(int i=0; iheight ? 0 : height-i;
square += wlength * hlength;
}
long result = allRect - square;
return result;
}
}
[/java]
0 件のコメント:
コメントを投稿