최근 글 ✨

[SWEA] 2805 농작물 수확하기 Java

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV7GLXqKAWYDFAXB&categoryId=AV7GLXqKAWYDFAXB&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

[코드]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class CodingTest {
	static int[][] map;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			int N = sc.nextInt();
			int start = N / 2; // 시작점
			int end = N / 2; // 끝점
			int sum = 0;
			map = new int[N][N];
			for (int i = 0; i < N; i++) {
				String str = sc.next();
				for (int j = 0; j < N; j++) {
					map[i][j] = str.charAt(j) - '0';
				}
			}

			for (int i = 0; i < N; i++) {
				for (int j = start; j <= end; j++) {
					sum += map[i][j];
				}
				if (i < N / 2) { // 중간보다 작을 때 
					start--;
					end++;
				}
				if (i >= N / 2) { // 중간보다 클 때 
					end--;
					start++;
				}
			}
			System.out.println("#" + test_case + " " + sum);
		}
	}
}