-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path81.cpp
More file actions
56 lines (48 loc) · 1.89 KB
/
Copy path81.cpp
File metadata and controls
56 lines (48 loc) · 1.89 KB
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
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
//❶ 각 물건의 단위 무게당 가치를 계산하여 items 벡터에 추가
void calculate_unit_value(vector<vector<double>> &items) {
for (auto &item : items) {
item.push_back(item[1] / item[0]);
}
}
//❷ 단위 무게당 가치가 높은 순으로 물건을 정렬
void sort_by_unit_value(vector<vector<double>> &items) {
sort(items.begin(), items.end(), [](const vector<double> &a, const vector<double> &b) {
return a[2] > b[2];
});
}
double knapsack(vector<vector<double>> &items, double weight_limit) {
double total_value = 0; //❸ 선택한 물건들의 총 가치를 저장
double remaining_weight = weight_limit; //❹남은 무게 한도를 저장
//❺ items을 순회하며 물건을 선택
for (const auto &item : items) {
if (item[0] <= remaining_weight) {
//❻ 남은 무게 한도 내에서 물건을 통째로 선택
total_value += item[1];
remaining_weight -= item[0];
} else {
//❼ 남은 무게 한도가 물건의 무게보다 작으면 쪼개서 일부분만 선택
double fraction = remaining_weight / item[0];
total_value += item[1] * fraction;
break; //❽ 이미 배낭의 무게 한도를 모두 사용한 경우
}
}
return total_value;
}
double solution(vector<vector<double>> items, double weight_limit) {
calculate_unit_value(items);
sort_by_unit_value(items);
//➒ 배낭의 무게 한도 내에서 담을 수 있는 물건들의 최대 가치를 반환(소수점 둘째자리 까지만 나타냄)
return round(knapsack(items, weight_limit) * 100) / 100;
}
//아래 코드는 테스트 코드 입니다.
#include <iostream>
int main()
{
cout << solution({{10, 19}, {7, 10}, {6, 10}}, 15) << endl; //출력값 : 27.3333
cout << solution({{10, 60}, {20, 100}, {30, 120}}, 50) << endl; //출력값 : 240
return 0;
}