java
N x N 크기의 농장이 격자 모양으로 구성되어 있으며, 각 격자 칸에는 농작물의 가치가 표시되어 있다. 이 농장에서는 특별히 중앙에서 시작하여 마름모 모양으로 농작물을 수확한다. 마름모 구역 내의 농작물 가치의 합을 구하는 문제
각 줄에서 중앙으로부터의 거리에 따라 수확 범위를 결정하고, 해당 범위 내의 값들을 더하여 문제를 풀었다.
마름모 형태의 수확 구역을 계산하는 것이 처음에는 복잡해 보였지만, 중앙으로부터의 거리 개념을 사용하니 조금 수월하게 풀 수 있었습니다.
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=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
int N = sc.nextInt();
int[][] farm = new int[N][N];
int total = 0;
for (int i = 0; i < N; i++) {
String row = sc.next();
for (int j = 0; j < N; j++) {
farm[i][j] = row.charAt(j) - '0';
}
}
int center = N / 2;
for (int i = 0; i < N; i++) {
int distance = Math.abs(center - i);
for (int j = distance; j < N - distance; j++) {
total += farm[i][j];
}
}
System.out.println("#" + test_case + " " + total);
}
sc.close();
}
}
Java들어가며 소프트웨어를 개발할 때 메모리 관리 방식은 프로그램의 안정성과 성능을 좌우하는 핵심 요소입니다. 특히 자바스크립트,…
들어가며 소프트웨어 개발자는 코드가 어떻게 실행되는지 정확히 이해해야 할 필요가 있습니다. 우리가 작성한 프로그램은 결국…
서론 현대 웹 애플리케이션 아키텍처에서 웹 서버(Web Server) 와 웹 애플리케이션 서버(WAS, Web Application Server)…
HTTP 헤더(Header)란? HTTP(Header)는 클라이언트와 서버 간에 교환되는 메타데이터로, 요청(Request)과 응답(Response)에 부가적인 정보를 실어 나르는 역할을…
Readable Code: 읽기 좋은 코드를 작성하는 사고법Practical Testing: 실용적인 테스트 가이드 강의와 함께한 인프런 워밍업 클럽…
Readable Code: 읽기 좋은 코드를 작성하는 사고법Practical Testing: 실용적인 테스트 가이드 강의와 함께한 인프런 워밍업 클럽…
View Comments
Howdy just wanted to give you a quick heads up. The words in your content seem to be running off the
screen in Firefox. I'm not sure if this is a format
issue or something to do with browser compatibility but I thought I'd post to let you know.
The style and design look great though! Hope you get the issue fixed soon. Thanks
Thank you for letting me know. I will look into it.
It’s the best time to make some plans for the long
run and it’s time to be happy. I’ve read this publish and if I may I want to suggest you few attention-grabbing
issues or tips. Perhaps you can write subsequent articles relating to
this article. I want to learn even more things about it!