큰 어려움없이 구할 수 있었습니다. 코드는 다음과 같습니다:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int Answer;
int main(int argc, char** argv)
{
int T, test_case;
/*
The freopen function below opens input.txt file in read only mode, and afterward,
the program will read from input.txt file instead of standard(keyboard) input.
To test your program, you may save input data in input.txt file,
and use freopen function to read from the file when using cin function.
You may remove the comment symbols(//) in the below statement and use it.
Use #include<cstdio> or #include <stdio.h> to use the function in your program.
But before submission, you must remove the freopen function or rewrite comment symbols(//).
*/
// freopen("input.txt", "r", stdin);
cin >> T;
for(test_case = 0; test_case < T; test_case++)
{
Answer = 0;
int n, k, temp ;
vector<int> grade ;
cin >> n >> k ;
for(int i = 0 ; i < n ; i++){
cin >> temp ;
grade.push_back(temp) ;
}
sort(grade.rbegin(), grade.rend()) ;
for(int i = 0 ; i < k ; i++)
Answer += grade[i] ;
/////////////////////////////////////////////////////////////////////////////////////////////
/*
Implement your algorithm here.
The answer to the case will be stored in variable Answer.
*/
/////////////////////////////////////////////////////////////////////////////////////////////
// Print the answer to standard output(screen).
cout << "Case #" << test_case+1 << endl;
cout << Answer << endl;
}
return 0;//Your program should return 0 on normal termination.
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/Codeground/P3_Study_Test.cpp
훈수 및 조언은 언제든 환영입니다.