1. 격자판 최대합
5*5 격자판에 아래롸 같이 숫자가 적혀있습니다.
N*N의 격자판이 주어지면 각 행의 합, 각 열의 합, 두 대각선의 합 중 가 장 큰 합을 출력합니다.
import java.util.Scanner;
public class Main {
public int solution(int[][] numberArray, int number) {
int result = 0;
int x, y;
for (int i = 0; i < number; i++) {
x = y = 0;
for (int j = 0; j < number; j++) {
x += numberArray[i][j];
y += numberArray[j][i];
}
result = Math.max(result, x);
result = Math.max(result, y);
}
x = y = 0;
for (int i = 0; i < number; i++) {
x += numberArray[i][i];
y += numberArray[i][number - i - 1];
}
result = Math.max(result, x);
result = Math.max(result, y);
return result;
}
public static void main(String[] args) {
Main main = new Main();
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int[][] numberArray = new int[number][number];
for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
numberArray[i][j] = sc.nextInt();
}
}
System.out.println(main.solution(numberArray, number));
}
}
반응형
'알고리즘' 카테고리의 다른 글
11월 13일 1문제 - Java : Array (0) | 2021.11.13 |
---|---|
11월 3일 1문제 - Java : Array (0) | 2021.11.03 |
10월 21일 4문제 - Java : Array (0) | 2021.10.25 |
10월 20일 4문제 - Java : Array (0) | 2021.10.20 |
10월 17일 2문제 - Java (0) | 2021.10.17 |