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 ...
随机推荐
- Node.js学习资料整理
了解node,node主要能干啥? Node.js究竟是什么?:http://www.ibm.com/developerworks/cn/opensource/os-nodejs/ nodejs教程: ...
- WIN10主动推升级,有点意思
不论正与盗,皆推升级,是否一样可用?
- PHP 判断客户端请求是 Android 还是 IOS
<?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad ...
- 一款非常炫酷的jQuery动态随机背景滚动特效
一款非常炫酷的jQuery动态随机背景滚动特效 图片背景会不停息的滚动,带有那种漂浮的视觉效果,小圈圈飘动. 更好的是还兼容IE6浏览器,大伙可以好好研究研究. 适用浏览器:IE6.IE7.IE8.3 ...
- JavaScript中使用console调试程序的坑
上DEMO a = {key1: [1, 2], 'key2': {'key4': '11'}, 'key3': [1, 2]} console.info(1,a) a.key2.key4 = '22 ...
- WPF与DevExpress之旅-序言
随着.NET技术的发展,从之前的WINFORM转向到WPF是我们技术改革的必然趋势.WPF能给人带来震撼的视觉体验,也能更加规范我们的开发模式,与传统的WINFORM开发来说具有革命性的意义.DevE ...
- GGS: Sybase to Oracle
Step 1: Start the GGSCI on Source and Target Source Target Oracle GoldenGate Command Interpreter for ...
- IOS UIWebView截获html并修改便签内容,宽度自适应
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服 ...
- 显示 mac 隐藏文件
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...
- 常见的装置与其在Linux当中的档名
需要特别留意的是硬盘机(不论是IDE/SCSI/U盘都一样),每个磁碟机的磁盘分区(partition)不同时, 其磁碟档名还会改变呢!下一小节我们会介绍磁盘分区的相关概念啦!需要特别注意的是磁带机的 ...