algorithm
[프로그래머스] 최고의 집합
지제로
2022. 9. 17. 20:42
풀이 과정
1. 예외사항인 집합이 존재하지 않는 경우 처리한다.
2. s를 n에 맞게 나눈다.
3. s에서 나온 숫자만큼을 뺀다.
4. 해당 숫자를 answer에 넣는다.
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, int s) {
vector<int> answer;
if (s / n <= 0) {
answer.push_back(-1);
return answer;
}
for (int i = n; i > 0; i--) {
int tmp = s / i;
s -= tmp;
answer.push_back(tmp);
}
return answer;
}