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 ...
随机推荐
- linux命令行解析函数介绍
函数原型: int getopt(int argc,char * const argv[ ],const char * optstring); 给定了命令参数的数量 ( ...
- linux编程:环境表
每个进程在启动的时候都会收到一张环境表.环境表是由一个字符指针数组组成,每个指针包含一个以NULL结束的字符串的地址,全局变量environ包含了指针数组的地址: extern char **envi ...
- c#获取今天星期几
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek)
- js----方法是否加括号的问题
在我们js编写程序的时候,我们会写很多函数然后调用它们,那么这些函数调用的时候什么时候加()什么时候不加()?记住以下几个要点. (1)函数做参数时都不要括号. function fun(e) { a ...
- IDEA笔记
快捷键: 查找类:ctrl + shif + R (eclipse)查找文件:double shift查找文件中的变量名和方法:ctrl + H (eclipse)system.out:输入 sout ...
- Delphi XE5教程2:程序组织
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...
- Oracle 摘去数据块的面纱
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F 00018000h 6 A2 0 0 0c 0 80 3 8b 61 15 0 0 0 3 4 type frmt spa ...
- C# 生成简单验证码
网站登录总是会用到验证码,生成验证码对于C#来说很简单.因为有专门封装好的GDI+类可以直接调用使用具体代码如下 using System; using System.Collections.Gene ...
- c++中new分配动态数组
变长一维数组 这里说的变长数组是指在编译时不能确定数组长度,程序在运行时需要动态分配内存空间的数组.实现变长数组最简单的是变长一维数组,你可以这样做: //文件名: array01.cpp ...
- 进程和cpu绑定
#include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/sysinfo ...