본문 바로가기

Code Practice

[Codeground] 연습문제 - #3. 시험 공부(Java)




[문제 요약]

 - 총 N 개의 과목 중 K 개의 과목만 골라 "최대 합계 점수"를 구하는 프로그램


[문제 조건]

 - 제한 시간 : 전체 테스트 케이스는 20개 이하, 전체 수행 시간은 1초 이내(Java 2초 이내)

 - 메모리 사용 제한 : heap, global, static 총계 256MB, stack 1MB


[입력 조건]

 - 입력 파일의 첫째 줄에는 테스트 케이스 개수를 나타내는 자연수 T ( 1 <= T <= 20 )

 - 각 테스트 케이스의 첫째 줄에는 총 과목의 수 N, 그리고 공부할 수 있는 과목의 수 K 나타냄 ( N은 20만 이하의 자연수)

 - 각 테스트 케이스의 두 번째 줄에는 N 개의 숫자들이 공백(빈칸)을 두고 주어짐

 - 각 숫자는 100 이하의 음이 아닌 정수, 공부했을 때 받을 수 있는 점수를 의미함


[출력 조건]

 - 각 테스트 케이스의 답을 순서대로 표준 출력

 - 첫 줄에는 "Case # T"를 출력. 이때 T는 테스트 케이스의 번호

 - 그다음 줄에는 K 개 과목의 "최대 합계 점수"를 출력함




[정답 문제 풀이]

 - 쉬운 문제다. 배열로 N개의 과목 점수를 받고 오름차순으로 정렬한 다음, 끝에서 K 번째 까지 합을 구하고 출력하면 된다. 여기서 for 문을 i가 1부터 K까지, N-i번째 배열의 값을 가져와 총합을 계산하면 된다.


[소스 코드]

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
You should use the statndard input/output
in order to receive a score properly.
Do not use file input and output
Please be very careful. 
*/
 
import java.util.Arrays;
import java.util.Scanner;
 
/*
   As the name of the class should be Solution , using Solution.java as the filename is recommended.
   In any case, you can execute your program by running 'java Solution' command.
 */
class Solution {
    static int Answer;
 
    public static void main(String args[]) throws Exception    {
        /*
           The method below means that the program will read from input.txt, instead of standard(keyboard) input.
           To test your program, you may save input data in input.txt file,
           and call below method to read from the file when using nextInt() method.
           You may remove the comment symbols(//) in the below statement and use it.
           But before submission, you must remove the freopen function or rewrite comment symbols(//).
         */        
 
        /*
           Make new scanner from standard input System.in, and read data.
         */
        Scanner sc = new Scanner(System.in);
        //Scanner sc = new Scanner(new FileInputStream("input.txt"));
 
        int T = sc.nextInt();
        for(int test_case = 0; test_case < T; test_case++) {
 
            Answer = 0;
            /////////////////////////////////////////////////////////////////////////////////////////////
            /*
               Implement your algorithm here.
               The answer to the case will be stored in variable Answer.
             */
            
            int N = sc.nextInt();
            int[] score = new int[N];
            int K = sc.nextInt();
            
            for(int i = 0; i < N; i++) {
                score[i] = sc.nextInt();
            }
            
            Arrays.sort(score);
            
            int totalScore = 0;
            
            for(int i = 1; i <= K; i++) {
                totalScore = totalScore + score[N-i];
            }
            
            Answer = totalScore;
            /////////////////////////////////////////////////////////////////////////////////////////////
 
 
            // Print the answer to standard output(screen).
            System.out.println("Case #"+(test_case+1));
            System.out.println(Answer);
        }
    }
}
cs


[후기]

 - 쉬운 문제이긴 했지만 한번에 정답을 맞춰서 뿌듯했다... ㅎㅎ