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 ...
随机推荐
- 把div固定在网页顶部
很多网站都有把某一块固定在顶部的功能,今天上网搜搜然后自己又写了一遍,贴出来给大家看看,哪天用到的时候自己也可以随时看看 <!DOCTYPE html PUBLIC "-//W3C// ...
- JavaScript正则实战
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- Android源代码编译——编译
环境准备好了,代码下载完了,据说make一下就可以. 当然也可以配置一下环境变量: 编译初始化,在终端中执行: source build/envsetup.sh 选择编译目标,在终端中执行下面的命令: ...
- laravel--belongsTo关联
1.第一个是要引入的模型类 格式这样 belongsTo 第二个参数是拿自己这个模型表的 哪个字段 去匹配 要关联的qualified表里的哪个ID 默认是拿qualified_id去匹配,前面的是对 ...
- what is WLAN ? when and why we need use it ?
无线局域网络(Wireless Local Area Networks: WLAN)是相当便利的数据传输系统,它利用射频(Radio Frequency: RF)的技术,取代旧式碍手碍脚的双绞铜线(C ...
- linux下获得块设备大小
运行结果如下 jackie@Ubuntu:~/work/0602$ sudo ./a.out /dev/sda/dev/sda3907029168,2000398934016 //BLKGETSIZE ...
- oracle中的dual表详解
oracle中的dual表详解 1.DUAL表的用途 Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中 --查看当前连接用户 SQL> s ...
- net use命令详细解释
1)建立空连接: net use \\IP\ipc$ "" /user:"" (一定要注意:这一行命令中包含了3个空格) 2)建立非空连接: net use \ ...
- 跨过slf4j和logback,直接晋级log4j 2
今年一直关注log4j 2,但至今还没有出正式版.等不及了,今天正式向大家介绍一下log4j的升级框架,log4j 2. log4j,相信大家都熟悉,至今对java影响最大的logging系统,至今仍 ...
- 人生规划和GTD——“知”、“得”与“合”
写在前面:非常不错的一篇GTD入门以及人生目标与规划的文章,值得收藏. 本文来自 零折 作为<小强升职记>的读书感悟,给我自己.作为分享,也送给或许需要的给你. 我不知道你是否真的需要.但 ...