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); } } } |