Number of Rectangles in a Grid
Project Euler 85: Investigating the number of rectangles in a rectangular grid
Number of Rectangles in a Grid
Given a grid of size m x n, calculate the total number of rectangles contained in this rectangle. All integer sizes and positions are counted.
Examples:
numberOfRectangles(3, 2) == 18
numberOfRectangles(4, 4) == 100
Here is how the 3x2 grid works (Thanks to GiacomoSorbi for the idea):
1 rectangle of size 3x2:
[][][]
[][][]
2 rectangles of size 3x1:
[][][]
4 rectangles of size 2x1:
[][]
2 rectangles of size 2x2
[][]
[][]
3 rectangles of size 1x2:
[]
[]
6 rectangles of size 1x1:
[]
As you can see (1 + 2 + 4 + 2 + 3 + 6) = 18, and is the solution for the 3x2 grid.
There is a very simple solution to this!
public class Grid {
public static int NumberOfRectangles(int m, int n) {
// Your code here!
int sum = ;
for (int i = ; i <= m; i++)
{
for (int j = ; j <= n; j++)
{
sum += (m - i + ) * (n - j + );
}
}
return sum;
}
}
Number of Rectangles in a Grid的更多相关文章
- leetcode 750. Number Of Corner Rectangles
Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner rectang ...
- 【LeetCode】750. Number Of Corner Rectangles 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 2016 ACM Amman Collegiate Programming Contest D Rectangles
Rectangles time limit per test 5 seconds memory limit per test 256 megabytes input standard input ou ...
- [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)
Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...
- White Rectangles[HDU1510]
White Rectangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Counting Rectangles
Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...
- HDU1510 White rectangles
White Rectangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- UVA - 10574 Counting Rectangles
Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...
- Rectangles hdu2461容斥定理
Rectangles Time Limit: 5000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 手机网站中 限制图片宽度 JS图片等比例缩放
<script type="text/javascript"> $(function () { var w = $(".content-co").w ...
- c# 类型初始值设定项引发异常
今天使用VS2010编译c#程序,编译顺利通过,点击运行启动程序,弹框提示如题错误.断点调试,程序甚至都没有进入main函数!!查阅网上资料,几种分析如下(1)反射机制 (2)app.config文件 ...
- 去掉iphone手机滑动默认行为
/*去掉iphone手机滑动默认行为*/ $('body').on('touchmove', function (event) { event.preventDefault(); });
- SQL Server 2000 “用户XX已经存在” 处理方法
-- 目前遇到这个问题都是在切换服务器时发生的. 旧服务器备份的数据库还原到新服务器,都会遇到这种问题 --切决方案如下: -- 查找孤立用户列表 EXECUTE sp_change_users_lo ...
- DemoExample
<HTML> <HEAD> <TITLE>使用递归求解斐波那契数列</TITLE> </HEAD> <BODY> <?ph ...
- easy ui datagrid 数据绑定
1.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- EXTJS 4.2 资料 控件之checkboxgroup的用法(动态数据)
在开发中遇到两种情况:第一在新增窗体时,要动态加载CheckBox项:第二在修改时不但需要加载所有CheckBox项,还要并且选中之前新增时的选项 如图这是在修改页面的效果: 1.在新增窗体中动态加载 ...
- 微软职位内部推荐-Senior Data Scientist
微软近期Open的职位: Extracting accurate, insightful and actionable information from data is part art and pa ...
- velocity语法
1.声明变量 #set($var = XXX) 右边可以是以下的内容 Variable reference String literal Property reference Method refer ...
- drupal CMS
http://drupalchina.cn/ https://www.drupal.org