回複:一個極具挑戰的編程問題

來源: 布衣之才 2011-08-24 21:24:14 [] [博客] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (1898 bytes)
回答: 一個極具挑戰的編程問題難割難舍中國心2011-08-19 09:54:40

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

請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉/移除任何Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock/Adblock plus

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”