HR30 Day 11: 2D Arrays

Jmnote (토론 | 기여)님의 2018년 8월 24일 (금) 19:47 판 (→‎PHP)

1 개요

Day 11: 2D Arrays
해커랭크 30 Days of Code
문제 풀이
10-19 Day e
HR30 Day 10: Binary Numbers

HR30 Day 11: 2D Arrays

HR30 Day 12: Inheritance

HR30 Day 13: Abstract Classes

HR30 Day 14: Scope

HR30 Day 15: Linked List

HR30 Day 16: Exceptions - String to Integer

HR30 Day 17: More Exceptions

HR30 Day 18: Queues and Stacks

HR30 Day 19: Interfaces

2 Java

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int[][] arr = new int[6][6];
        for (int i = 0; i < 6; i++) {
            String[] arrRowItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            for (int j = 0; j < 6; j++) {
                int arrItem = Integer.parseInt(arrRowItems[j]);
                arr[i][j] = arrItem;
            }
        }
        scanner.close();
        
        int sum;
        int max = -100;
        for(int i=1; i<5; i++) {
            for(int j=1; j<5; j++) {
                sum = arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1]
                    + arr[i][j]
                    + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1];
                if( sum > max ) max = sum;
            }
        }
        System.out.println( max );
    }
}

3 PHP

<?php
$stdin = fopen("php://stdin", "r");
$arr = array();
for ($i = 0; $i < 6; $i++) {
    fscanf($stdin, "%[^\n]", $arr_temp);
    $arr[] = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
}
fclose($stdin);
$max = -100;
for($i=1; $i<count($arr)-1; $i++) {
    for($j=1; $j<count($arr[0])-1; $j++) {
        $sum = $arr[$i-1][$j-1] + $arr[$i-1][$j] + $arr[$i-1][$j+1]
            + $arr[$i][$j]
            + $arr[$i+1][$j-1] + $arr[$i+1][$j] + $arr[$i+1][$j+1];
        if( $sum > $max ) $max = $sum;
    }
}
echo $max;

4 Python

#!/bin/python3
import math
import os
import random
import re
import sys

if __name__ == '__main__':
    arr = []
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
    mx = -100
    for i in range(1,len(arr)-1):
        for j in range(1,len(arr[0])-1):
            sm = arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1] \
                + arr[i][j] \
                + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1]
            if( sm > mx ): mx = sm
    print( mx )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}