VB的演算法問題,VB的乙個演算法問題

2021-03-10 22:16:07 字數 7483 閱讀 9215

1樓:網海1書生

你換個思路嘛!用累加就很容易解決了,用不著考內慮有多少種組合!

容dim x as single

if check1.value=1 then x=x+val(text1.text)

if check2.value=1 then x=x+val(text2.text)

if check3.value=1 then x=x+val(text3.text)

if check4.value=1 then x=x+val(text4.text)

if check5.value=1 then x=x+val(text5.text)

text0.text=x

2樓:

用二進位制解復

決吧。選中制

的科目為1,未選中的科目為0,那總共是2^5=32種。去掉全都不選,剩31種。

然後你就可以用 select case 來解決這個問題了。

在實際應用中,你還可以考慮直接按位計算來解決,連select case都不用。

例如:假設所選科目資訊儲存在變數k裡面

那麼結果可以是 結果= 科目1成績 x (k and 2^1) + 科目2成績 x (k and 2^2) + ……

這樣如果沒選該科目,乘以零的結果也必然為零,自然就忽略掉了。

3樓:匿名使用者

給你個思路:

bai先定義個總du分數變數

zhi fen,再建立乙個checkbox陣列,daocheck(1),check(2)......然後用for ***x 迴圈遍

專歷陣列,在屬迴圈內,if chedk(i).value=1,then fen =fen+val(text(i).text) ,在迴圈外,text0.text=fen。

希望能幫到你!

vb乙個演算法問題。

4樓:匿名使用者

樓主到現在還沒結貼證明還沒找到所求,正好有空,我就補上vb6的碼:

問題考點:數字組合演算法,就用最簡單的遞迴吧。

你先建三text,text1的屬性:scrollbars設為2 multiline設定true, text1縱軸拉長點,text2、text3左右拉長點點(好看數),以及一***mand1

option explicit

dim sourcearray, dstarray, strsum as string, i***ist() as integer

private sub ***bine(a, n as integer, m as integer, b, mm as integer) '遞迴演算法

dim i%, j%, strsum$, sum%

for i = n to m step -1

b(m - 1) = i - 1

if m > 1 then

call ***bine(a, i - 1, m - 1, b, mm)

else

sum = 0

strsum = ""

for j = mm - 1 to 0 step -1

sum = sum + a(b(j))

strsum = strsum & a(b(j)) & "+"

next j

i***ist(sum) = 1

strsum = left$(strsum, len(strsum) - 1) & "=" & sum

text1 = text1 & strsum & vbcrlf

doevents

end if

next i

end sub

private sub ***mand1_click()

dim ncount as integer, arraymax as integer, arraymin as integer, strtemp as string, inttemp as integer

dim i as integer, sum as integer

ncount = inputbox("請輸入該系列數字的總數:")

arraymin = inputbox("請輸入該系列數字的最小值:")

do while arraymax <= arraymin

if arraymax <> 0 then

arraymax = inputbox("最大值必須比最小值" & arraymin & "大,請輸入該系列數字的最大值:")

else

arraymax = inputbox("請輸入該系列數字的最大值:")

end if

loop

randomize

text1 = ""

text2 = "該系列數字具體為:"

for i = 1 to ncount

inttemp = int(rnd * (arraymax - arraymin + 1) + arraymin) '隨機產生數字

text2 = text2 & inttemp & " "

strtemp = strtemp & inttemp & " "

next

sourcearray = split(rtrim$(strtemp))

dstarray = split(rtrim$(strtemp))

for i = 0 to ncount - 1

sum = sum + sourcearray(i)

next i

redim i***ist(sum) as integer

text3 = "計算中,請稍等......"

for i = 1 to ncount

call ***bine(sourcearray, ncount, i, dstarray, i)

next

text3 = "可能的和一定沒下列數字:"

for i = 0 to sum

if i***ist(i) = 0 then

text3 = text3 & i & " "

end if

next

text3 = text3 & "以及大於" & sum & "的所有數" '最終結果

end sub

5樓:世慷

//就像你上面的資料3,

7,4,5,4,8,20.用下面的**得到的結果是(不包含)

1、2、6、9、12、13、16、17、21、24、25、26、28、29、32、33、35、36、37、38、40、41、42、44、45、46、47、48、49、50、51、

//原理就是塞選法.這個演算法是很笨的。其實你可以先考慮排下序試試

private i***it(0) as boolean

private n, sumtemp as integer 'n表示第幾層

private data(0) as integer

private sub mainrun(byval s() as integer)

dim ret as string = ""

dim i as integer

data = s

n = data.length - 1

for i = 0 to n

sumtemp += data(i)

next

redim i***it(sumtemp)

sumtemp = 0

while n > 0

callrun(0, n)

n -= 1

end while

for i = 1 to i***it.length - 1

if i***it(i) = false then

ret += i.tostring + "、"

end if

next

messagebox.show(ret)

end sub

private sub callrun(byval index as integer, byval n2 as integer)

dim i, temp as integer

if n2 = 1 then

for i = index to data.length - 1

i***it(sumtemp + data(i)) = true

next

sumtemp = 0

else

temp = sumtemp

n2 -= 1

for i = index to data.length - n2 - 1

sumtemp = temp + data(index)

callrun(index + 1, n2)

next

end if

end sub

private sub test()//測試函式

dim s() as integer =

mainrun(s)

end sub

6樓:匿名使用者

做乙個不定長的陣列

輸入資料到陣列

從0到全部數的和迴圈

從陣列第乙個到最後乙個迴圈對比

如果不是 則顯示此數

7樓:

表達得不夠清楚 還不是沒有理解你的意圖

vb演算法問題,乙個堆數字中湊出指定數字

8樓:匿名使用者

資料20-30個,這個高效與低效,執行時間應該差不了多少吧!

我給個簡單的演算法:

1,將這些資料讀入乙個陣列a()

2,對陣列內的資料進行公升序排序

3,依次從陣列中取乙個數與其他數進行求和

1)先算兩個數之和100的。從a(0)開始,當a(i)>50時,即可跳出迴圈,開始進行三個數之和是100的計算

2)當a(i)>34時,跳出迴圈,進行四個數之和是100的計算3)當a(i)>25時,跳出迴圈,進行五個數之和是100的計算依次類推

9樓:葉成桐

將一堆數字看成乙個陣列,從第乙個開始,遍歷剩下的數,如果第乙個和第n個的和=100,就輸出;遍歷完後,從第二個在開始,遍歷剩下的,如果第二個和第n個的和=100,...............

10樓:匿名使用者

這是個求子集合加總問題(subset sum problem)。是演算法理論中比較有名的np問題。

有幾種經典解法:

1.組合論。 有所有集合元素的組合,然後求和與和目標比較。 方法簡單,但演算法複雜度高,當集合數較大,比如≥ 15後,速度明顯慢;

2. 動態規劃。遞迴求解,屬於典型的divide and conquer方案。

3. 回溯法(backtracking),子集合屬於這個裡面的乙個特例。 -- 雖然也要遞迴,但相較上面的方法,在集合比較大的時候,也能保持不錯的效率。

下面給出回溯法的vb**(vb 2010)。

private sub output(byref ta() as integer, byval ta_size as integer)

dim ra(ta_size - 1) as integer 'differ from c/c++

array.copy(ta, ra, ta_size)

dim converter = new converter(of integer, string)(function(num) num.tostring)

dim str = string.join("+", array.convertall(ra, converter))

lbsubset.items.add(str)

end sub

private sub getsubsetsum(byref sa() as integer, byref ta() as integer,

byval sa_size as integer, byval ta_size as integer,

byval sum as integer, byval **t_node as integer, byval target as integer)

dim i as integer

if target = sum then

output(ta, ta_size)

if **t_node + 1 < sa_size and sum - sa(**t_node) + sa(**t_node + 1) <= target then

getsubsetsum(sa, ta, sa_size, ta_size - 1, sum - sa(**t_node), **t_node + 1, target)

end if

return

else

if **t_node < sa_size and sum + sa(**t_node) <= target then

for i = **t_node to sa_size - 1 ' differ from c/c++

ta(ta_size) = sa(i)

if sum + sa(i) <= target then

getsubsetsum(sa, ta, sa_size, ta_size + 1, sum + sa(i), i + 1, target)

end if

next i

end if

end if

end sub

private sub generatesubsets(byref sa() as integer, byval size as integer, byval target as integer)

dim ta(size - 1) as integer

dim total as integer = 0

array.sort(sa)

total = sa.sum

if (sa(0) <= target) and (total >= target) then

getsubsetsum(sa, ta, size, 0, 0, 0, target)

end if

end sub

private sub btnstart_click(sender as system.object, e as system.eventargs) handles btnstart.

click

dim size as integer = 15

dim target as integer = 10

dim data() as integer

dim i as integer

lbsubset.items.clear()

redim data(size - 1) 'differ from c/c++

for i = lbound(data) to ubound(data)

data(i) = i + 1

next i

generatesubsets(data, size, target)

end sub

vb中的演算法問題,VB乙個演算法問題。

你的舉例很簡bai單的啊,用dufor 迴圈就可以實現。private sub mand1 click i 0 for a 1 to 9 for b 3 to 9 if a b 15 then list1.additem a a b b i i 1 next b next a print a b 1...

簡單VB問題,乙個簡單VB問題

上面的兄弟是考lz的改錯技術嗎?我改了一下macrotian給的 private sub command1 click dim pro as double dim i as integer n text1.text pro 1 for i 1 to n if i mod 2 0 then pro p...

排列組合的演算法問題,乙個排列組合的演算法問題

貌似是求組合的,現寫了個比較簡單的,可得出任意n選m的組合 include include void print int beg,int end void c int n,int m else if m n return p int malloc sizeof int m for i 1 i m i...