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. C#对象转JSON字符串和JSON字符串转对象

    namespace Net.String.ConsoleApplication { using System; using System.Data; using System.Collections; ...

  2. 安装完MySQL后必须要调整的10项配置

    2014年02月10日11:11 来源:开源中国 作者:BoydWang, 美好的2014, DrZ 编辑:徐志远 标签: 配置 , mysql , 数据库 [IT168 评论]当我们被人雇来监测My ...

  3. btrace 实践笔记

    btrace简介:     btrace 是一个使用在JAVA平台上面的,安全的,动态跟踪工具.它一般用于动态跟踪正在运行的jAVA程序.     使用说明在这里.下载地址在这里.     下载的时候 ...

  4. linux计划任务运行php文件的方法分享

    在linux下,借助crontab,设置计划任务每天6点10分执行filename.php文件,写入一行时间到log日志中. 创建计划任务的脚本: dos2unix /path/to/filename ...

  5. ubuntu14.04 中文输入法无法使用

    说下我的解决方法吧,我是忘了在All Settings -> Text Entry 的 Input sources to use中添加Chinese(Pinyin)了,添加后就好了. from: ...

  6. ubuntu下下载并安装H265(hm.x.x代码和X265代码)

    H265,现今是High Efficiency Video Coding的别称,详细的概述见维基百科,详细的开发见官方网站. 一.下载并编译官方的测试源码HM.x.x: 1 ubuntu下安装svn: ...

  7. JS和JSP的区别

    最近很多同学在纠结于名词缩写之间的相似性,因此本人也来写一篇,讲讲JS和JSP的区别. SUN首先发展出SERVLET,其功能比较强劲,体系设计也很先进,只是,它输出HTML语句还是采用了老的CGI方 ...

  8. Hadoop学习---安装部署

    hadoop框架 Hadoop使用主/从(Master/Slave)架构,主要角色有NameNode,DataNode,secondary NameNode,JobTracker,TaskTracke ...

  9. 后台启动mysql ,redis

    mysqld_safe --user=mysql & redis.conf daemonize no修改为daemonize yes

  10. EXTJS 4.2 添加滚动条

    bodyStyle: 'overflow-x:hidden; overflow-y:scroll',//显示滚动 文章来源:http://www.cnblogs.com/exmyth/archive/ ...