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 |