Project Euler 11 Largest product in a grid
题意:在这个20×20方阵中,四个在同一方向(从下至上、从上至下、从右至左、从左至右或者对角线)上相邻的数的乘积最大是多少?
思路:暴力去枚举以 ( x , y ) 为中心拓展的四个方向
/*************************************************************************
> File Name: euler011.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月25日 星期日 09时35分02秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#define MAX_N 26
#define max(a,b) a > b ? a : b
void input_mat(int32_t mat[][MAX_N]) {
for(int32_t i = 3 ; i < 23 ; i++) {
for(int32_t j = 3 ; j < 23 ; j++) {
scanf("%d",&mat[i][j]);
}
}
}
void solve(int32_t mat[][MAX_N]) {
int32_t ans = 0;
int32_t dx[4] = { 0 , 1 , 1 , 1 } , dy[4] = { 1 , 0 , 1 , -1 }; // 定义方向数组
for(int32_t i = 3 ; i < 23 ; i++){
for(int32_t j = 3 ; j < 23 ; j++){
for(int32_t num = 0 ; num < 4 ; num++){ // 四个方向进行遍历
int32_t p = mat[i][j];
for(int32_t k = 1 ; k < 4 ; k++){
p *= mat[ i + k*dx[num] ][ j + k*dy[num] ];
}
ans = max( ans , p );
}
}
}
printf("%d\n",ans);
}
int32_t main() {
int32_t mat[MAX_N][MAX_N] = {0};
freopen("PE11input.txt","r",stdin);
input_mat(mat);
solve(mat);
return 0;
}
Project Euler 11 Largest product in a grid的更多相关文章
- Project Euler 8 Largest product in a series
题意:寻找这 1000 个数中相邻 13 个数相乘积最大的值 思路:首先想到的是暴力,但是还可以利用之前记录过的数值,什么意思呢?即在计算 2 - 14 后,再计算 3 - 15 时完全可以利用之前计 ...
- R语言学习——欧拉计划(11)Largest product in a grid
Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 0 ...
- Largest product in a grid
这个比前面的要复杂点,但找对了规律,还是可以的. 我逻辑思维不强,只好画图来数数列的下标了. 分四次计算,存入最大值. 左右一次,上下一次,左斜一次,右斜一次. In the 2020 grid be ...
- Project Euler 99:Largest exponential 最大的幂
Largest exponential Comparing two numbers written in index form like 211 and 37 is not difficult, as ...
- Project Euler Problem 8-Largest product in a series
直接暴力 写完才想到,代码写矬了,扫一遍就出结果了,哪还用我写的这么麻烦 ss = '''73167176531330624919225119674426574742355349194934 9698 ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- 【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 × ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 036:Project Euler 008:1000位数字中相邻13个数字最大的乘积
本题来自 Project Euler 第8题:https://projecteuler.net/problem=8 # Project Euler: Problem 8: Largest produc ...
随机推荐
- Method and apparatus for an atomic operation in a parallel computing environment
A method and apparatus for a atomic operation is described. A method comprises receiving a first pro ...
- javascript--闭包与this
理解javascript中的闭包与this http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html http: ...
- MySQL Workbench常用快捷键及修改快捷键的方法
常用快捷键: 1.执行整篇sql脚本:[Ctrl]+[Shift]+[Enter] 2.执行当前行:[Ctrl]+[Enter] 3.注释/取消注释:[Ctrl]+[/] 4.格式化sql语句(美化s ...
- Linux查看文件内容命令:more(转)
Linux more命令类似cat ,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按b键就会往回(back)一页显示,而且还有搜寻字串的功 ...
- 淘宝内部分享:MySQL & MariaDB性能优化
发表于2015-01-20 16:26| 28875次阅读| 来源mysql.taobao.org| 22 条评论| 作者淘宝数据库团队 MySQL性能优化淘宝数据库 摘要:MySQL是目前使用最多的 ...
- ul,li不能左右居中的问题
近期帮朋友做一个他们公司的商品站点,用到了曾经学到的html+css技术,当然做站点少不了Javascript和jquery这些..... 这个功能主要实现了导航条里面的条目是居中的.所以声明了ul, ...
- Java&&(面试题)初始化顺序总结
1 无继承情况下的Java初始化顺序: class Sample { Sample(String s) { System.out.println(s) ...
- hdu 1011 树型dp
#include <cstdio> #include <iostream> #include <cstring> #include <vector> u ...
- [Angular] Increasing Performance by using Pipe
For example you make a function to get rating; getRating(score: number): string { let rating: string ...
- 一种加快在苹果app store中上架的方法
预计近期苹果app应用上架的比較多,审核比較慢,如今一个app从提交到上架短则7.8天.长则2.3个星期.我在实际上线应用时,总结了一个简单有用的小技巧,能够加快上架时间,近期使用这样的方法后.我们基 ...