#include<bits/stdc++.h>usingnamespace std;int n, m;//物品数量,背包容量 structinfo{
double w, v, ratio;//重量,价值 }a[60];boolcmp(info x, info y){
return x.ratio > y.ratio;}intmain(){
cin >> n >> m;for(int i =1; i <= n; i ++){
cin >> a[i].w >> a[i].v;
a[i].ratio = a[i].v / a[i].w;}sort(a +1, a +1+ n, cmp);double w =0, v =0;for(int i =1; i <= n; i ++){
if(w + a[i].w < m){
v += a[i].v;
w += a[i].w;}else{
v += a[i].ratio *(m - w);break;}}printf("%.2f", v);return0;}
01背包(模板题)
#include<bits/stdc++.h>usingnamespace std;int n, m;int f[209];intmain(){
cin >> m >> n;for(int i =1; i <= n; i ++){
int w, v;
cin >> w >> v;for(int j = m; j >= w; j --){
f[j]=max(f[j], f[j - w]+ v);}}
cout << f[m];return0;}
集装箱装载
#include<bits/stdc++.h>usingnamespace std;int n, m;int f[33000];intmain(){
cin >> n >> m;for(int i =1; i <= n; i ++){
int w;
cin >> w;for(int j = m; j >= w; j --){
f[j]=max(f[j], f[j - w]+ w);}}
cout << f[m];return0;}/*
1 9
10
*/
#include<bits/stdc++.h>usingnamespace std;int n, m;bool f[33000];intmain(){
cin >> n >> m;
f[0]=true;for(int i =1; i <= n; i ++){
int w;
cin >> w;for(int j = m; j >= w; j --){
f[j]= f[j]|| f[j - w];}}for(int j = m; j >=0; j --){
if(f[j]){
cout << j;return0;}}return0;}/*
1 9
10
*/
小D的跨国贸易(贪心)
#include<bits/stdc++.h>usingnamespace std;int m, n, w[109];intmain(){
cin >> m >> n;for(int i