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 ...
随机推荐
- 转载 - Tarjan算法(求SCC)
出处:http://blog.csdn.net/xinghongduo/article/details/6195337 说到以Tarjan命名的算法,我们经常提到的有3个,其中就包括本文所介绍的求强连 ...
- C# SortedDictionary<TKey, TValue> 类
表示依据键进行排序的键/值对的集合. https://msdn.microsoft.com/zh-cn/library/f7fta44c.aspx
- _DataStructure_C_Impl:基数排序
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...
- python TypeError: 'builtin_function_or_method' object is not iterable keys
statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statin ...
- 【JMeter连接SQLServer】採用window鉴权方式连接(原创)
大家都知道Jmeter能够连接各种数据库.这方面我也不多说了,假设你还不知道怎么连接的话.能够參看我看的另外一篇博文.这边有具体的介绍 http://blog.csdn.net/lzqinfen/ar ...
- [POJ 2282] The Counting Problem
[题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...
- Node.js:目录
ylbtech-Node.js:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. http://www.runoob.com/nodejs/nodejs ...
- md5 c# unicode 互换(原创)
php 代码 $input='中国'; $result= md5($input); $temp=iconv("UTF-8", "UTF16LE", $input ...
- js与jquery基础知识对比(一)---2017-05-06
用表格做的,想要对比的内容一目了然,红色部分为重点 js jquery 取元素 id: document.getElementById("aa"); 取到的是dom对象 cla ...
- 访问修饰符相关注意点(protected子类友好)
注意:protected表示只有在子类和同包中可以访问. 需要注意的是,在其他包中,若是创建了父类的对象,但是父类对象访问不了自己类里面用protected修饰的属性,只能由子类访问父类的protec ...