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. TweenMax动画库学习(五)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  2. 网站如何做到完全不需要jQuery

    jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的 ...

  3. memcached 使用积累

    1.memcahed在windows上的安装 . 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached . 在终端(也即cmd命令界面)下输入 ‘c:\memc ...

  4. 解决IE6中ajax ‘aborted’错误请求中断

    给a标签绑定了一个click事件用来触发ajax请求,在IE6中,请求时常会被中断,在其他浏览器中都一切正常. 在IE6中使用Fiddler2和httpWatch监视请求,经常会出现”aborted” ...

  5. Demo学习: ClientInfo

    ClientInfo 获取客户端环境参数,从0.9版本开始新增了TUniClientInfoRec对象,可以得到客户端的一些信息,之前为了获取浏览器版本号需要自己写函数,现在可以直接使用TUniCli ...

  6. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  7. Oracle分析函数 — sum, rollup, cube, grouping用法

    本文通过例子展示sum, rollup, cube, grouping的用法. //首先建score表 create table score( class  nvarchar2(20), course ...

  8. GNU_makefile_template

    #g++ compiler: options # -std=c++0x enables ISO C++ 11 standard # -I.. pulls in the Version_test.h f ...

  9. Linux内核树的建立-基于ubuntu系统

    刚看 O'REILLY 写的<LINUX 设备驱动程序>时.作者一再强调在编写驱动程序时必须 建立内核树.先前的内核只需要有一套内核头文件就够了,但因为2.6的内核模块吆喝内核源码树中的目 ...

  10. asp.net mvc Ajax.BeginForm不能异步刷新,或转到新页面,或页面还是刷新了,的原因(或解决办法)(转)

    昨天搞了一下午的Ajax.BeginForm都没能实现异步刷新,一直将返回的数据提交到新的页面,在网上搜了n多方法都不行,问了n多人都没能搞定,今天大一早,就意外的被我发现了问题所在. 布局页: &l ...