如何计算出指定数字列表的组合数及组合明细

有一堆數字如 5,6,10,50,90,10,7,2,6,1 共10個 總計187 ,但有限制條件,不管怎麼加不可以超過100且要接近100

問:有幾種組合
問:如何電腦自動配置,列出組合明細
假如可以組合 4組
如第1組 5,6,10,50,7,2,6,1
如第2組 10+90

 

通过以下递归函数,可以实现

public subroutine wf_dg (long a[], integer start, string as, long al_sum, long max, long min, datastore ads)
if al_sum >= max then return
if start > upperbound(a) then return
int i; long ll_sum, row; string ls
for i = start to upperbound(a)
    ll_sum = al_sum + a[i]
    if ll_sum <= max then
        if as = '' then
            ls = string(a[i])
        else
            ls = as + ',' + string(a[i])
        end if
        if ll_sum >= min then
            row = ads.insertrow(0);ads.setitem(row, 's', ls);ads.setitem(row, 't', ll_sum)
        end if
        if ll_sum = max then continue
        wf_dg(a, i + 1, ls, ll_sum, max, min, ads)
    end if
next
end subroutine

递归函数的调用入口如下, lds用来存储组合明细, dw_1是窗口中用来显示组合明细的控件。

datastore lds
lds = create datastore
lds.dataobject = 'd_result'
string ls, ls_sub
ls = mle_1.text + ','
long ll[], i
do while true
    i = posw(ls, ',')
    if i <= 0 then exit
    if i = 1 then 
        ls = midw(ls, 2); continue
    end if
    ls_sub = leftw(ls, i - 1)
    ls = midw(ls, i + 1)
    if not isnumber(ls_sub) then continue //非数字
   ll[upperbound(ll) + 1] = long(ls_sub)
loop
wf_dg(ll, 1, '', 0, 100, 90, lds) //统计大于等于90小于等于100的所有组合
lds.setsort("t d, s d"); lds.sort()
lds.rowscopy( 1, lds.rowcount(), primary!, dw_1, 1, primary!)
destroy lds


 附上各对象的导出内容  d_result.srd

$PBExportHeader$d_result.srd
release 9;
datawindow(units=0 timer_interval=0 color=1073741824 processing=1 HTMLDW=no print.printername="" print.documentname="" print.orientation = 0 print.margin.left = 110 print.margin.right = 110 print.margin.top = 96 print.margin.bottom = 96 print.paper.source = 0 print.paper.size = 0 print.canusedefaultprinter=yes print.prompt=no print.buttons=no print.preview.buttons=no print.cliptext=no print.overrideprintjob=no print.collate=yes hidegrayline=no grid.lines=0 )
header(height=80 color="536870912" )
summary(height=0 color="536870912" )
footer(height=0 color="536870912" )
detail(height=76 color="536870912" )
table(column=(type=char(1000) updatewhereclause=yes name=s dbname="s" )
 column=(type=long updatewhereclause=yes name=t dbname="t" )
 )
text(band=header alignment="2" text="组合情况" border="0" color="33554432" x="155" y="12" height="56" width="1157" html.valueishtml="0"  name=s_t visible="1"  font.face="宋体" font.height="-9" font.weight="400"  font.family="0" font.pitch="2" font.charset="134" background.mode="1" background.color="536870912" )
text(band=header alignment="2" text="总和" border="0" color="33554432" x="1321" y="12" height="56" width="256" html.valueishtml="0"  name=t_t visible="1"  font.face="宋体" font.height="-9" font.weight="400"  font.family="0" font.pitch="2" font.charset="134" background.mode="1" background.color="536870912" )
column(band=detail id=2 alignment="1" tabsequence=20 border="0" color="33554432" x="1321" y="12" height="68" width="256" format="[general]" html.valueishtml="0"  name=t visible="1" edit.limit=0 edit.case=any edit.focusrectangle=no edit.autoselect=yes edit.autohscroll=yes edit.imemode=0  font.face="宋体" font.height="-9" font.weight="400"  font.family="2" font.pitch="2" font.charset="134" background.mode="1" background.color="536870912" )
column(band=detail id=1 alignment="0" tabsequence=10 border="0" color="33554432" x="155" y="12" height="68" width="1157" format="[general]" html.valueishtml="0"  name=s visible="1" edit.limit=0 edit.case=any edit.focusrectangle=no edit.autoselect=yes edit.autohscroll=yes edit.imemode=0  font.face="宋体" font.height="-9" font.weight="400"  font.family="2" font.pitch="2" font.charset="134" background.mode="1" background.color="536870912" )
compute(band=detail alignment="2" expression="getrow()"border="0" color="33554432" x="9" y="12" height="60" width="137" format="[GENERAL]" html.valueishtml="0"  name=compute_1 visible="1"  font.face="宋体" font.height="-9" font.weight="400"  font.family="0" font.pitch="2" font.charset="134" background.mode="1" background.color="536870912" )
htmltable(border="1" )
htmlgen(clientevents="1" clientvalidation="1" clientcomputedfields="1" clientformatting="0" clientscriptable="0" generatejavascript="1" encodeselflinkargs="1" netscapelayers="0" )
export.xml(headgroups="1" includewhitespace="0" metadatatype=0 savemetadata=0 )
import.xml()
export.pdf(method=0 distill.custompostscript="0" xslfop.print="0" )
 


 w.srw

$PBExportHeader$w.srw
forward
global type w from window
end type
type st_3 from statictext within w
end type
type st_2 from statictext within w
end type
type st_1 from statictext within w
end type
type sle_2 from singlelineedit within w
end type
type sle_1 from singlelineedit within w
end type
type dw_1 from datawindow within w
end type
type cb_1 from commandbutton within w
end type
type mle_1 from multilineedit within w
end type
type browseinfo from structure within w
end type
end forward

Type BROWSEINFO From structure
    Long   hOwner
    Long   pidlRoot
    String pszDisplayName
    String lpszTitle
    Long   ulFlags
    Long   lpfn
    Long   lParam
    Long   iImage
End Type

global type w from window
integer width = 3173
integer height = 2092
boolean titlebar = true
string title = "Untitled"
boolean controlmenu = true
boolean minbox = true
boolean maxbox = true
boolean resizable = true
long backcolor = 67108864
string icon = "AppIcon!"
boolean center = true
st_3 st_3
st_2 st_2
st_1 st_1
sle_2 sle_2
sle_1 sle_1
dw_1 dw_1
cb_1 cb_1
mle_1 mle_1
end type
global w w

type prototypes
FUNCTION ulong waveOutSetVolume(ulong uDeviceID,ulong dwVolume) LIBRARY "winmm.dll"
 
FUNCTION ulong waveOutGetVolume(ulong uDeviceID,ref ulong lpdwVolume) LIBRARY "winmm.dll"
 
FUNCTION ulong waveOutGetID(ulong hWaveOut,ref ulong lpuDeviceID) LIBRARY "winmm.dll"
  
FUNCTION ulong waveOutClose(ulong hWaveOut) LIBRARY "winmm.dll"
 
//FUNCTION ulong waveOutOpen(ref ulong lphWaveOut,ulong uDeviceID,ref WAVEFORMAT lpFormat,ulong dwCallback,ulong dwInstance,ulong dwFlags) LIBRARY "winmm.dll"


FUNCTION Long SHBrowseForFolder (REF BROWSEINFO lpBrowseInfo)LIBRARY "shell32.dll" ALIAS FOR "SHBrowseForFolderA"
FUNCTION Long SHGetPathFromIDList(Long pidList, REF string lpBuffer)LIBRARY "shell32.dll" ALIAS FOR "SHGetPathFromIDListA"
function ulong CopyMem(ref ulong hMem, ref string source, ulong length) library "kernel32" alias for "RtlMoveMemory"

end prototypes

type variables
string is_sn
blob ib_photo_old

end variables

forward prototypes
public subroutine wf_dg (long a[], integer start, string as, long al_sum, long max, long min, datastore ads)
public function long wf_insertrow (datawindow adw, long rows)
end prototypes

public subroutine wf_dg (long a[], integer start, string as, long al_sum, long max, long min, datastore ads);if al_sum >= max then return
if start > upperbound(a) then return
int i; long ll_sum, row; string ls
for i = start to upperbound(a)
	if posw(',' + as + ',' , ',' + string(a[i]) + ',') > 0 then continue
	ll_sum = al_sum + a[i]
	if ll_sum <= max then
		if as = '' then
			ls = string(a[i])
		else
			ls = as + ',' + string(a[i])
		end if
		if ll_sum >= min then
			row = ads.insertrow(0);ads.setitem(row, 's', ls);ads.setitem(row, 't', ll_sum)
		end if
		if ll_sum = max then continue
		wf_dg(a, i + 1, ls, ll_sum, max, min, ads)
	end if
next
end subroutine

public function long wf_insertrow (datawindow adw, long rows);//====================================================================
// 函数: wf_insertrow()
//--------------------------------------------------------------------
// 描述:	在adw的尾部快速插入rows行(可以瞬间插入几十万条记录)
//--------------------------------------------------------------------
// 参数:
// 	value	datawindow	adw 	
// 	value	long      	rows	
//--------------------------------------------------------------------
// 返回:  long
//--------------------------------------------------------------------
// 作者:			日期: 2011.04.11
//====================================================================
long ll_rows, row_start, rowcount_now
adw.setredraw(false)
row_start = adw.insertrow(0)
ll_rows ++ //已经插入一行了
do while true
	rowcount_now = adw.RowCount()
	adw.RowsCopy(row_start,  min(rowcount_now - row_start, rows - ll_rows - 1) + row_start, Primary!, adw, rowcount_now + 1, Primary!)
	ll_rows += min(rowcount_now - row_start, rows - ll_rows - 1) + 1
	if ll_rows >= rows then exit
loop
adw.setredraw(true)
return 1
end function

on w.create
this.st_3=create st_3
this.st_2=create st_2
this.st_1=create st_1
this.sle_2=create sle_2
this.sle_1=create sle_1
this.dw_1=create dw_1
this.cb_1=create cb_1
this.mle_1=create mle_1
this.Control[]={this.st_3,&
this.st_2,&
this.st_1,&
this.sle_2,&
this.sle_1,&
this.dw_1,&
this.cb_1,&
this.mle_1}
end on

on w.destroy
destroy(this.st_3)
destroy(this.st_2)
destroy(this.st_1)
destroy(this.sle_2)
destroy(this.sle_1)
destroy(this.dw_1)
destroy(this.cb_1)
destroy(this.mle_1)
end on

type st_3 from statictext within w
integer x = 27
integer y = 56
integer width = 526
integer height = 48
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
long backcolor = 67108864
string text = "数字串:"
boolean focusrectangle = false
end type

type st_2 from statictext within w
integer x = 27
integer y = 460
integer width = 535
integer height = 48
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
long backcolor = 67108864
string text = "和 - 必须大于等于:"
boolean focusrectangle = false
end type

type st_1 from statictext within w
integer x = 27
integer y = 308
integer width = 526
integer height = 48
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
long backcolor = 67108864
string text = "和 - 接近但不大于:"
boolean focusrectangle = false
end type

type sle_2 from singlelineedit within w
integer x = 562
integer y = 444
integer width = 768
integer height = 84
integer taborder = 30
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
string text = "90"
borderstyle borderstyle = stylelowered!
end type

type sle_1 from singlelineedit within w
integer x = 562
integer y = 292
integer width = 768
integer height = 84
integer taborder = 20
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
string text = "100"
borderstyle borderstyle = stylelowered!
end type

type dw_1 from datawindow within w
integer x = 1381
integer y = 40
integer width = 1719
integer height = 1924
integer taborder = 20
string title = "none"
string dataobject = "d_result"
boolean hscrollbar = true
boolean vscrollbar = true
boolean livescroll = true
borderstyle borderstyle = stylelowered!
end type

type cb_1 from commandbutton within w
integer x = 965
integer y = 592
integer width = 366
integer height = 112
integer taborder = 20
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
string text = "求解(&A)"
end type

event clicked;datastore lds
lds = create datastore
lds.dataobject = 'd_result'
string ls, ls_sub
ls = mle_1.text + ','
long ll[], i
do while true
	i = posw(ls, ',')
	if i <= 0 then exit
	if i = 1 then 
		ls = midw(ls, 2); continue
	end if
	ls_sub = leftw(ls, i - 1)
	ls = midw(ls, i + 1)
	if not isnumber(ls_sub) then continue //非数字
	ll[upperbound(ll) + 1] = long(ls_sub)
loop
wf_dg(ll, 1, '', 0, long(sle_1.text), long(sle_2.text), lds)
lds.setsort("t d, s d"); lds.sort()
lds.rowscopy( 1, lds.rowcount(), primary!, dw_1, 1, primary!)
destroy lds

end event

type mle_1 from multilineedit within w
integer x = 562
integer y = 44
integer width = 768
integer height = 172
integer taborder = 10
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
long textcolor = 33554432
string text = "5,6,10,50,90,10,7,2,6,1"
boolean vscrollbar = true
boolean autovscroll = true
borderstyle borderstyle = stylelowered!
end type


 

你可能感兴趣的:(String,Integer,library,border,alignment,subroutine)