반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
05-18 06:40
관리 메뉴

ImJay

[SWEA/Java] 1859. 백만 장자 프로젝트 본문

SW Expert Academy/D2

[SWEA/Java] 1859. 백만 장자 프로젝트

ImJay 2024. 1. 20. 18:23
반응형

[SWEA/Java] 1859. 백만 장자 프로젝트

 

SW Expert Academy

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

swexpertacademy.com


풀이

import java.io.*;
import java.util.StringTokenizer;

public class Solution {
    public static void main(String[] args) throws IOException {
        // 입력을 받기 위한 BufferedReader와 출력을 위한 BufferedWriter 생성
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        // 테스트 케이스의 개수를 입력받음
        int testCase = Integer.parseInt(br.readLine());

        // 각 테스트 케이스에 대한 반복
        for (int t = 1; t <= testCase; t++) {
            // 배열의 크기를 입력받음
            int n = Integer.parseInt(br.readLine());
            
            // 배열의 요소들을 문자열로 입력받고 StringTokenizer를 이용하여 정수 배열로 변환
            String input = br.readLine();
            StringTokenizer st = new StringTokenizer(input);
            int[] list = new int[n];

            // 정수 배열로 변환한 값을 배열에 저장
            for (int i = 0; i < n; i++) {
                list[i] = Integer.parseInt(st.nextToken());
            }

            // 각 위치에서 최대값과의 차이를 저장할 배열 및 초기값 설정
            int max = 0;
            int[] sum = new int[n];

            // 배열을 역순으로 순회하면서 최대값 갱신과 차이를 계산하여 저장
            for (int j = n - 1; j >= 0; j--) {
                if (max < list[j]) max = list[j];
                sum[j] = max - list[j];
            }

            // 결과값 계산
            long res = 0;
            for (int i = 0; i < n; i++)
                res += sum[i];

            // 결과 출력
            bw.write("#" + t + " " + res + "\n");
        }

        // 출력 버퍼 비우고 닫기
        bw.flush();
        bw.close();
    }
}
반응형
Comments