Project Euler 85 :Counting rectangles 数长方形
By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:

Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the nearest solution.
如果数得足够仔细,能看出在一个3乘2的长方形网格中包含有18个不同大小的长方形,如下图所示:

尽管没有一个长方形网格中包含有恰好两百万个长方形,但有许多长方形网格中包含的长方形数目接近两百万,求其中最接近这一数目的长方形网格的面积
解题
有下面内容:
对于任意矩形M*N
其中1*1的矩阵有M*N个
1*2的矩阵有M*(N-1)个
2*1的矩阵有(M-1)*N个
实际上只要确定小矩阵左上角顶点在大矩形中的位置,这个矩阵的位置就唯一确定了
所有在任意矩形M*N中,矩阵i*j有(M-i+1)*(N-j+1)个
所以对于M*N的矩阵总的矩阵数量是:
int num = 0;
for(int i =1;i<= m;i++){
for(int j =1;j<= n;j++){
num += (m-i + 1)*(n - j+1);
}
}
更让人想不到是是直接计算矩阵的数量:
num = (m+1)*m*(n+1)*n/4
Java
package Level3;
import java.util.Random; public class PE085{ static void run() {
int limit = 100;
int close = Integer.MAX_VALUE;
int area = 0;
for(int m =1;m< limit ;m++){
for(int n = 1;n< limit ;n++){
int num = grid_num(m,n);
if (num>2000000)
break;
if( Math.abs(num - 2000000 ) < Math.abs(close - 2000000)){
close = num;
area = n*m;
}
}
}
System.out.println(area);
}
public static int grid_num2(int m , int n){
int num = 0;
num = (m+1)*m*(n+1)*n/4;
return num;
}
// 2772
// running time=0s0ms
public static int grid_num(int m , int n){
int num = 0;
for(int i =1;i<= m;i++){
for(int j =1;j<= n;j++){
num += (m-i + 1)*(n - j+1);
}
}
return num;
}
// 2772
// running time=0s20ms public static void main(String[] args){
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
}
}
你说是不是很流氓,这个规律,我怎么那么聪慧的会发现?
Python
# coding=gbk
import time as time t0 = time.time() def run():
limit = 100
close = 0
area = 0
for m in range(1,limit):
for n in range(1,limit):
num = grid_num(m,n)
if num>2000000:break
if abs(num - 2000000) < abs(close -2000000):
close = num
area = n*m
print area def grid_num(m ,n):
count = 0
for i in range(1,m+1):
for j in range(1,n+1):
count += (m-i+1)*(n-j+1)
return count run()
t1 = time.time()
print "running time=",(t1-t0),"s" #
# running time= 1.19499993324 s
Project Euler 85 :Counting rectangles 数长方形的更多相关文章
- Project Euler 19 Counting Sundays( 蔡勒公式计算星期数 )
题意:在二十世纪(1901年1月1日到2000年12月31日)中,有多少个月的1号是星期天? 蔡勒公式:计算 ( year , month , day ) 是星期几 以下图片仅供学习! /****** ...
- project euler 19: Counting Sundays
import datetime count = 0 for y in range(1901,2001): for m in range(1,13): if datetime.datetime(y,m, ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
- Python练习题 030:Project Euler 002:偶数斐波那契数之和
本题来自 Project Euler 第2题:https://projecteuler.net/problem=2 # Each new term in the Fibonacci sequence ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
随机推荐
- ArcGIS API for JavaScript介绍
ArcGIS API for JavaScript中的类是按照模块组织的,主要包含esri.esri/geometry.esri/renderers.esri/symbols.esri/symbols ...
- Silverlight中嵌套html、swf、pdf
1.MainPage.xaml <UserControl x:Class="SilverlightClient.MainPage" xmlns="http://sc ...
- TortoiseGit 安装和使用的图文教程
TortoiseGit.SourceTree都是Windows下不错的Git客户端工具,下面介绍一下TortoiseGit安装和使用的方法. 安装TortoiseGit并使用它需要两个软件:Torto ...
- (三)开始在OJ上添加签到功能
在了解完OJ文件下的各个文件夹的主要作用后,我们开始往里面添加东西(其实只要知道各文件夹是干什么的后,添加东西也变得非常简单了) 一 在数据库中添加对应功能的字段. 我们这个学期才刚开数据库这门课,所 ...
- linux发展前景如何?
2014-01-09 18:54Linux将不会取代Windows成为主流的桌面操作环境, 但它很有可能在信息接入设备中独霸天下. 为什么Linux无法取代Windows呢?最主要的原因是大多数最终用 ...
- Codeforces 559A 第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...
- 【jquery】javaScript中prototype的妙用 巧妙运用prototype属性原型链创建对象
prototype 可以有好多有优化实现方法 http://blog.csdn.net/liuqiwen0512/article/details/8089690 在 JavaScript 中,每个函 ...
- RCP,TCP,C/S,B/S
RCP: RICH CLIENT PROGRAM 胖客户端 TCP: THIN CLIENT PROGRAM 瘦客户端 CS: CLIENT SERVER 客户端/服务 ...
- 清除SQL Server执行计划
有时需要调试SQL语句的性能, 需要不断的执行SQL语句, 可是多次执行同一条语句的时候,SQL Server 会缓存表的数据,结果就测不出来 实际的 SQL 的性能 用以下SQL可以清除缓存数据 D ...
- Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...