✔ 내 답 -> 성공
import java.util.*;
public class P2_9 {
public static int solution(int n, int[][] arr) {
int max=0;
int sum=0;
//가로
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum+=arr[i][j];
}
if(max<sum) max=sum;
sum=0;
}
//세로
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum+=arr[j][i];
}
if(max<sum) max=sum;
sum=0;
}
// 대각선
for(int i=0;i<n;i++){
sum+=arr[i][i];
}
if(max<sum) max=sum;
sum=0;
for(int i=0;i<n;i++){
sum+=arr[i][n-1-i];
}
if(max<sum) max=sum;
return max;
}
public static void main(String[] args){
Scanner kb= new Scanner(System.in);
int n = kb.nextInt();
int[][] arr = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j] = kb.nextInt();
}
}
System.out.println(solution(n,arr));
}
}
매우 지저분하고 복잡하지만 성공 강의보고 다시 해봐야겠다..
✔ 강의도 똑같은데 반복되는 for문을 합쳐버림
import java.util.*;
public class P2_9 {
public static int solution(int n, int[][] arr) {
int max=0, sum_ga=0, sum_se=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum_ga+=arr[i][j];
sum_se+=arr[j][i];
}
if(sum_ga<sum_se) max=sum_se;
else max=sum_ga;
sum_ga=0;
sum_se=0;
}
for(int i=0;i<n;i++){
sum_ga+=arr[i][i];
sum_se+=arr[i][n-1-i];
}
if(sum_ga<sum_se) max=sum_se;
else max=sum_ga;
return max;
}
public static void main(String[] args){
Scanner kb= new Scanner(System.in);
int n = kb.nextInt();
int[][] arr = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j] = kb.nextInt();
}
}
System.out.println(solution(n,arr));
}
}