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 ...
随机推荐
- 安装GCC for Red Hat Enterprise Linux Server release 6(64位)
http://www.cnblogs.com/emanlee/archive/2012/08/11/2633895.html
- iOS_25_彩票设置的cell的数据源模型的封装
组模型的封装 SettingGroup // // SettingGroup.h // 25_彩票 // // Created by beyond on 14-8-28. // Copyright ( ...
- POJ 1987
T_T为毛会这样子,我的写就是过不了,....... 其实这题不难,很容易想到吧,我一开始也想着用枚举这类方法,但复杂度实在不敢想,没想到,真的是用这种方法.. 今天学了一个叫树的重心,可以使分治的子 ...
- Codeforces Round #313 (Div. 2) 560D Equivalent Strings(dos)
D. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 某P2P开发商ERP系统核心业务介绍
之前说到.某软件公司卖P2P系统的后台管理系统.号称"ERP",今天继续说说这个ERP的核心业务. 业务1:贷款审批流程 贷款审批.主要是针对线下人员的 ...
- CentOS 7通过yum安装fcitx五笔输入法
CentOS 7通过yum安装fcitx五笔输入法 下面通过了亲測: 1.设置源 Posted in Linux at 三月 5th, 2015 / No Comments ? 增加EPEL源 EPE ...
- kvc和kvo的使用情况的了解
了解cocoa:Cocoa是苹果公司为Mac OS X所创建的原生面向对象的API,是Mac OS X上五大API之中的一个(其他四个是Carbon.POSIX.X11和Java). 苹果的面向对象开 ...
- Android笔记之网络状态推断
1.首先当然得在 manifest 中加入检查网络状态的权限: <uses-permission android:name="android.permission.ACCESS_NET ...
- ubuntu清华源【转】
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 可以选择ubuntu的版本更新源.
- CH Round #46A 磁力块
还是一道好题的 对于一个磁石是否被吸引,有两个关键字:距离和质量.(二维偏序??) 好像是很厉害的分块姿势,先按第一关键字排序,在块中按第二关键字排 进行bfs,对于当前磁石,有1~k-1个块是第一关 ...