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 ...
随机推荐
- 前端自动化构建工具——gulp
gulp是基于流的前端自动化构建工具. 一.环境配置 gulp是基于nodejs的,所以没有 nodejs 环境的要先去安装好 然后给系统配上gulp环境 npm install -g gulp 再到 ...
- 优化过的redis封装类
转http://www.cnblogs.com/jackluo/p/3410192.html <?php /** * RedisCluster 群redius操作类 * * //创建连接 * $ ...
- 用“逐步排除”的方法定位Java服务线上“系统性”故障(转)
一.摘要 由于硬件问题.系统资源紧缺或者程序本身的BUG,Java服务在线上不可避免地会出现一些“系统性”故障,比如:服务性能明显下降.部分(或所 有)接口超时或卡死等.其中部分故障隐藏颇深,对运维和 ...
- openerp学习笔记 计算字段支持搜索
示例1: # -*- encoding: utf-8 -*-import poolerimport loggingimport netsvcimport toolslogger = netsvc.Lo ...
- Discuz X1.5 利用添加好友处存储xss进行蠕虫worm扩散
Discuz X1.5 在添加好友的地方有处存储xss,借助此处xss跟用户交互可以进行蠕虫指数扩散. 位置在添加好友处 x完之后的效果 点击后触发 ok 借助此存储xss,我们进行worm传播,dz ...
- MVC中System.InvalidOperationException: 传入字典的模型项的类型为“XXX”,但此字典需要类型“XXA”的模型项
出现此类错误的一个原因是Controller传过去的Model和View中的Model不是同一个Model
- SVN四部曲之SVN简单使用教程入门
1. 签出源代码到本机 在本机创建文件夹StartKit,右键点击Checkout,弹出如下图的窗体: 2. 2 在上图中URL of Repository:下的文本框中输 ...
- "=="和equals方法究竟有什么区别
(单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同, ...
- Android开发笔记一(hello world)
UI: <Button android:layout_width="wrap_content" android:layout_height="wrap_conten ...
- VBS基础篇 - 变量
VBScript只有一种数据类型 —— Variant,它是根据上下文来判断是数字还是字符串.因为Variant是VBScript中唯一的数据类型,所以它也是VBScript中所有函数的返回值的数据类 ...