Developing Myself Everyday
Published 2023. 11. 17. 14:04
2805. 농작물 수확하기 [D3] by Java SWEA
 

SW Expert Academy

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

swexpertacademy.com

 

문제와 관련된 내용은 위의 링크에서 확인할 수 있습니다.


 

 

 

 

1. 나의 풀이

이 문제는 특별할 것이 따로 없는, 단순한 계산문제입니다.

 

문제에서 주어진 마름모는 가장 가운데 열을 기준으로 위 아래가 동일한 형태를 가지고 있다는 것을 알 수 있습니다.

 

그렇기에 저는 가장 가운데 좌표에서부터 위 아래로 동시에 특정한 범위만큼 수확하도록 했습니다.

<code />
import java.util.Scanner; import java.util.stream.Stream; class Solution { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); int T; T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { int n = sc.nextInt(); String[][] harvests = new String[n][n]; for (int i = 0; i < n; i++) { harvests[i] = sc.next().split(""); } int mid = n / 2; int range = n / 2; int wholeHarvest = Stream.of(harvests[mid]).mapToInt(Integer::parseInt).sum(); for (int i = 1; i <= mid; i++) { wholeHarvest += Integer.parseInt(harvests[mid + i][mid]); wholeHarvest += Integer.parseInt(harvests[mid - i][mid]); for (int j = 1; j < range; j++) { wholeHarvest += Integer.parseInt(harvests[mid + i][mid + j]); wholeHarvest += Integer.parseInt(harvests[mid + i][mid - j]); wholeHarvest += Integer.parseInt(harvests[mid - i][mid + j]); wholeHarvest += Integer.parseInt(harvests[mid - i][mid - j]); } range--; } System.out.println("#" + test_case + " " + wholeHarvest); } } }
profile

Developing Myself Everyday

@배준형

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!