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
| #include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
int main ()
{
using std::begin;
using std::end;
const unsigned taille_boite = 150;
std::vector<unsigned> objets({10,120,30,40,55,90,130,150,70,65,35,20,15,180});
std::vector<std::vector<unsigned>>boites;
std::sort(begin(objets), end(objets), std::greater<unsigned>());
for(auto const & objet : objets){
bool affecte = false;
for(auto & boite : boites){
if(taille_boite - std::accumulate(begin(boite), end(boite), 0) >= objet){
boite.push_back(objet);
affecte = true;
break;
}
}
if(!affecte && objet <= taille_boite) {
boites.emplace_back(1, objet);
}
}
for (auto & boite : boites) {
for (auto & x : boite) {
std::cout << x << ' ';
}
std::cout << '\n';
}
} |
Partager