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的更多相关文章

  1. 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 ...

  2. 【LeetCode】750. Number Of Corner Rectangles 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  3. 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 ...

  4. [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)

    Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...

  5. White Rectangles[HDU1510]

    White Rectangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. Counting Rectangles

    Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...

  7. HDU1510 White rectangles

    White Rectangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. UVA - 10574 Counting Rectangles

    Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...

  9. Rectangles hdu2461容斥定理

    Rectangles Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 在Windows 7下面IIS7的安装和 配置ASP的正确方法

    在Windows 7下如何安装IIS7,以及IIS7在安装过程中的一些需要注意的设置,以及在IIS7下配置ASP的正确方法. 一.进入Windows 7的 控制面板,选择左侧的打开或关闭Windows ...

  2. Android 基础(设备显示密度/图片自适应

    1. 设备的 显示密度 是由 设备的尺寸 和 设备的分辨率 两个因素决定的. 相同分辨率设备的尺寸越大显示密度越小, 相同尺寸的设备,分辨率越高显示密度越高. 2. 显示密度等级:  160 / 24 ...

  3. epoll_create, epoll_ctl和epoll_wait

    参考代码 #include <iostream> #include <sys/socket.h> #include <sys/epoll.h> #include & ...

  4. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  5. HDU 4548 美素数

    Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“ ...

  6. 论Oracle字符集“转码”过程

    本文将通过实验来演示一下Oracle字符集“转码”的确认过程. 1.实验环境说明 客户端是Windows XP操作系统的SQL*Plus程序,客户端字符集是936(对应Oracle的ZHS16GBK字 ...

  7. oracle linux了解基本命令行

    1.      Linux的版本:核心(kernel)版本和发行(distribution)版本 2.      复制.删除和移动文件的命令 cp [选项] 源文件或目录  目标文件或目录 -R,-r ...

  8. NGUI系列教程八(监听NGUI的事件方法)

    NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定在按钮上,当按钮点击时就可以监听到,这种方法不太好很不 ...

  9. C# Windows - ListBox&CheckedListBox

    ListBox和CheckedListBox类的属性 属性 说明 SelectedIndex 这个值表明列表框中选中项的基于0的索引 ColumnWidth 在包含多个列的列表框中,这个属性指定列宽 ...

  10. WDS无线桥接

    因为放假回家,长时间不在家,家里也没什么人,所以也就没有网可以用.为了两个月办宽带又不值得,太过浪费了.于是就只能蹭网用了.当然,要和邻居打个招呼或者你能搞定密码的情况下不打招呼(嘿嘿...).但是有 ...