zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    }
    int32_t main() {
    	int32_t mat[MAX_N][MAX_N] = {0};
    	freopen("PE11input.txt","r",stdin);
    
    	input_mat(mat);
    	solve(mat);
    	return 0;
    }
  • 相关阅读:
    mysql 远程登陆不上
    hdu 5339 Untitled【搜索】
    SqlServer 书目
    passwordauthentication yes
    oracle 11g RAC ocfs2
    Oracle 11g RAC database on ASM, ACFS or OCFS2
    CentOS ips bonding
    Oracle 11g RAC features
    openStack 王者归来之 trivial matters
    openstack windows 2008 img
  • 原文地址:https://www.cnblogs.com/WArobot/p/7076260.html
Copyright © 2011-2022 走看看