codeground 연습문제 3

 이 문제는 간단한 sorting으로 해결 할 수 있다.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
import java.util.Arrays;

class Solution {
    static int Answer, T, N, K;
    static int[] subjects;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            N = sc.nextInt(); K = sc.nextInt();
            subjects = new int[N];Answer = 0;
            for (int j = 0; j < N; j++) {
                subjects[j] = sc.nextInt();
            }
            Arrays.sort(subjects);
            for (int j = N; j > N - K; j--) {
                Answer += subjects[j - 1];
            }
            System.out.println("Case #"+(i+1));
            System.out.println(Answer);
        }
    }
}

이 블로그의 인기 게시물

웹툰 무료로 볼 수 있는 사이트

BackJoon 1011, Fly me to the alpha centauri, 규칙 찾기 문제

BaekJoon 6591, 이항 쇼다운 조합문제