回复:一个极具挑战的编程问题
There might be flaws in the following logic. Just as a starting point for discussion.
Package is a class with a collection of Documents.
Package.Pages = sum of pages of all its Documents
The subroutine PackMax can be called recursively.
----------------------------
SendDocuments(MaxPagesPerPack, AllDocuments){
while AllDocuments.Lenght > 0{
package = PackMax(MaxPagesPerPack, AllDocuments)
AllDocuments.Remove(Package.Documents)
Send (Package)
}
}
Package PackMax(maxPages, sourceDocuments) {
ParentPackage = new Package(sourceDocuments[0])
if ParentPackage.Pages = maxPages or sourceDocument.Length = 1
return ParentPackage
restDocuments = sourceDocument[1, ...] // subset from 2end
chileMaxPages = maxPages - ParentPackage.Pages
childPackage1 = PackMax(childMaxPages, restDocuments)
while childPackage1.Pages < childMaxPages{
restDocuments = restDocuments[1, ....]
childPackage2 = PackMax(childMaxPages, restDocuments)
if childPackage2.Pages > childPackage1.Pages
childPackage1 = childPackage2
}
ParentPackage.Apend(childPackage1)
return ParentPackage
}
Start a new (parent) package from the document with most pages
if the package is not full, find a sub package with the most possible pages from the rest of documents that can fit into the parent package with the firs