1209 Sum 문제 링크
문제분석
- 각 행의 합, 각 열의 합, 각 대각선의 합 중 최댓값 구하기
- 예시 :

제약 사항
- 테스트 케이스는 총 10개
- 배열의 크기는 100X100으로 동일
- 각 행의 합은 integer 범위를 넘어가지 않음
- 동일한 최댓값이 있을 경우, 하나의 값만 출력
입력 조건
- 첫째 줄 : 테스트 케이스 번호
- 둘째 줄 : 2차원 배열의 각 행 값
출력 조건
- #부호 + 테스트 케이스 번호 + 공백 + 최댓값 출력
#1
- num[100][100] , R[100] , C[100] , result[4]
- for ( i = 100 )
- for ( j = 100 )
- R[j] += num[i][j];
- C[i] += num[i][j];
- if(i==j) result[2] += 1;
- if(i+j == 100) result[3] += 1;
- for ( i = 100 )
- result[0] = Math.max(result[0], R[i]);
- result[1] = Math.max(result[1], C[i]);
- for ( i = 1~4 )
- result[0] = Math.max(result[0], result[i]);
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
for(int test_case = 1; test_case <= 10; test_case++)
{
int T = sc.nextInt();
int[][] num = new int[100][100];
int[] R = new int[100];
int[] C = new int[100];
int[] result = new int[4];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
num[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
R[j] += num[i][j];
C[i] += num[i][j];
if(i==j) result[2] += 1;
if(i+j == 100) result[3] += 1;
}
}
for(int i=0; i<100; i++) {
result[0] = Math.max(result[0], R[i]);
result[1] = Math.max(result[1], C[i]);
}
for(int i=0; i<4; i++) {
result[0] = Math.max(result[0], result[i]);
}
System.out.println("#" + T + " " + result[0]);
}
}
}

- 이렇게 하면 테스트 케이스는 모두 통과인데

- 제출은 실패함
#2
if(i==j) result[2] += 1;
(i+j == 100) result[3] += 1;
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
for(int test_case = 1; test_case <= 10; test_case++)
{
int T = sc.nextInt();
int[][] num = new int[100][100];
int[] R = new int[100];
int[] C = new int[100];
int[] result = new int[4];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
num[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
C[i] += num[i][j];
R[j] += num[i][j];
if(i==j) result[2] += num[i][j];
if(i+j == 100) result[3] += num[i][j];
}
}
for(int i=0; i<100; i++) {
result[0] = Math.max(result[0], C[i]);
result[1] = Math.max(result[1], R[i]);
}
for(int i=1; i<4; i++) {
result[0] = Math.max(result[0], result[i]);
}
System.out.println("#" + T + " " + result[0]);
}
}
}

5/1 다시 풀어봄
#3
10분
import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
T=10;
for(int test_case = 1; test_case <= T; test_case++)
{
int N = sc.nextInt();
int[][] arr = new int[100][100];
for(int i=0; i<100; i++){
for(int j=0; j<100; j++) {
arr[i][j] = sc.nextInt();
}
}
int maxA = 0;
int A = 0;
int maxB = 0;
int B = 0;
int C = 0;
int D = 0;
for(int i=0; i<100; i++) {
A = 0;
B = 0;
for(int j=0; j<100; j++) {
A += arr[i][j];
B += arr[j][i];
if(i==j) C += arr[i][j];
if(i+j==99) D += arr[i][j];
}
maxA = Math.max(maxA, A);
maxB = Math.max(maxB, B);
}
int max = Math.max(maxA, Math.max(maxB, Math.max(C, D)));
System.out.println("#"+test_case+" "+max);
}
}
}
- 저번 코드는 배열에 더한 값을 다 넣어서 나중에 최댓값을 구했고
- 이번 코드는 max() 함수로 미리 최댓값을 구함
- 근데 코드 살펴보다가 알았는데
- 이전 코드에서 대각선 구하는 조건문을 (i+j == 100)로 설정함
- 맞긴 했는데 정확한 답은 아닌듯...