코테 준비/Java [백준] #11441. 합 구하기 (실버 3) - https://www.acmicpc.net/problem/11441 import java.io.*;import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(st.nextToken()); } int[] numsPrefixSum = new int[n]; numsPrefixSum[0] = nums[0]; for (int i = 1; i < n; i++) { numsPrefixSum[i] = numsPrefixSum[i-1] + nums[i]; } int m = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for (int k = 0; k < m; k++) { st = new StringTokenizer(br.readLine()); int i = Integer.parseInt(st.nextToken()); int j = Integer.parseInt(st.nextToken()); if (i == 1) { sb.append(numsPrefixSum[j-1]); sb.append("\n"); } else { sb.append(numsPrefixSum[j-1] - numsPrefixSum[i-2]); sb.append("\n"); } } System.out.println(sb); }} 주의할 점 ▼ 구간의 시작이 첫번째부터이면 앞에 빼줄 값이 없으므로 고려할 것! if (i == 1) {sb.append(numsPrefixSum[j-1]);sb.append("\n");} 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기피할 수 없다면 즐기는 자가 일류 Contents 당신이 좋아할만한 콘텐츠 [백준] #17608. 막대기 (브론즈 2) 2024.06.06 [백준] #2167. 2차원 배열의 합 (실버 5) 2024.06.05 [백준] #2851. 슈퍼 마리오 (브론즈 1) 2024.06.05 [백준] #2467. 용액 (골드 5) 2024.06.03 댓글 0 + 이전 댓글 더보기