子題1(11%):一個菜販有「肉」、「菜」、「蛋」、「果」4 類商品,在菜攤上「分類」排列出
售,各項商品可由輸入檔讀入其「每公斤利潤」。有一天,菜販忘了商品在菜攤的排列方式,
只知道今天各類商品由左至右出售的「公斤數」,其資料選手也可由輸入檔讀入。在不知道今
天商品排列的情況下,請選手將所有的排列方式逐一代入計算,依總利潤之高低順序,由高
而低,輸出所有排列方式及其總利潤。
輸入說明:
第1 行有4 組數字,以逗號隔開,分別表示「肉」、「菜」、「蛋」、「果」每公斤的利潤。
第2 行有4 組數字,以逗號隔開,表示今天菜攤由左至右各類商品出售之公斤數。
輸出說明:
依總利潤之高低順序,每1 行輸出1 組排列方式及其總利潤,資料間至少以1 個空白隔開。
若有總利潤相同者,其前後輸出順序不拘。
輸入範例:【檔名:in-4-1.txt】
70, 32, 18, 42
43, 24, 21, 39
輸出範例:【檔名:out-4-1.txt】
肉菜蛋果 5794
肉蛋菜果 5752
果菜蛋肉 5682
肉果蛋菜 5644
果蛋菜肉 5640
肉蛋果菜 5572
菜果蛋肉 5492
菜蛋果肉 5420
肉果菜蛋 5392
肉菜果蛋 5362
蛋果菜肉 5184
蛋菜果肉 5154
果肉蛋菜 5112
菜肉蛋果 5072
果蛋肉菜 4956
菜蛋肉果 4916
果肉菜蛋 4860
蛋肉菜果 4764
果菜肉蛋 4746
蛋菜肉果 4650
菜肉果蛋 4640
蛋肉果菜 4584
菜果肉蛋 4556
蛋果肉菜 4500
Dim R() As String
回覆刪除Dim N(4)
Dim KG() As String
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Line Input #1, X
R = Split(X, ",")
Line Input #1, Y
KG = Split(Y, ",")
N(0) = "肉": N(1) = "菜"
N(2) = "蛋": N(3) = "果"
Call AA("")
For i = 0 To (List1.ListCount - 1)
Call BB(List1.List(i))
Next i
List1.Clear
Do
Max = Val(Mid(List2.List(0), 6))
max2 = 0
For i = 1 To (List2.ListCount - 1)
If Max < Val(Mid(List2.List(i), 6)) Then
max2 = i: Max = Val(Mid(List2.List(i), 6))
End If
Next i
List1.AddItem List2.List(max2)
List2.RemoveItem (max2)
Loop Until List2.ListCount = 0
For i = 0 To (List1.ListCount - 1)
Print #2, List1.List(i)
Next i
Close #2
Close #1
End
End Sub
Sub AA(ByVal A) '肉菜蛋果 做所有可能
If Len(A) = 4 Then
List1.AddItem A
Else
For i = 0 To 3
If InStr(A, N(i)) = 0 Then Call AA(A & N(i))
Next i
End If
End Sub
Sub BB(A) '做計算
total = 0
For i = 0 To 3
Select Case Mid(A, i + 1, 1)
Case N(0): total = total + Val(R(0)) * Val(KG(i))
Case N(1): total = total + Val(R(1)) * Val(KG(i))
Case N(2): total = total + Val(R(2)) * Val(KG(i))
Case N(3): total = total + Val(R(3)) * Val(KG(i))
End Select
Next i
List2.AddItem A & " " & total
End Sub
Dim N(4), P(4), NN
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
NN = Split("空 肉 菜 蛋 果")
Input #1, N(1), N(2), N(3), N(4)
Input #1, P(1), P(2), P(3), P(4)
Call cot("")
Call cot3
Close
Close
End Sub
Sub cot(a)
If Len(a) = 4 Then
Call cot2(a)
Else
For i = 1 To 4
If InStr(a, NN(i)) = 0 Then Call cot(a & NN(i))
Next
End If
End Sub
Sub cot2(a)
For i = 1 To 4
m = Mid(a, i, 1)
Select Case m
Case "肉": ans = ans + N(1) * P(i)
Case "菜": ans = ans + N(2) * P(i)
Case "蛋": ans = ans + N(3) * P(i)
Case "果": ans = ans + N(4) * P(i)
End Select
Next
ans2 = ans & " " & a & " " & ans
List1.AddItem ans2
End Sub
Sub cot3()
For i = List1.ListCount - 1 To 0 Step -1
Print #2, Right(List1.List(i), 10)
Next
End Sub
佑、arro好,
回覆刪除你們這一題的排序組合的遞迴,都做得很好,但是,都得不到分數,你們都因為listbox熟了,想用listbox的sort方式,看樣子,要剎車一下了,該氣泡排序的,還是用氣泡吧。
因為你們不能確定那個數字有多長,你們都用了一定長度的,只要數字再長了,答案就不同了。
*****
請練習雙陣列(或者多陣列)的排序吧。
輸入20個同學的3科成績,以總分降冪輸出座號及每科成績及總分。
輸入:
回覆刪除1,9,1,20
2,18,31,64
3,76,55,39
4,12,38,69
5,25,100,58
6,86,65,35
7,13,58,57
8,52,12,41
9,98,94,29
10,90,73,28
11,65,90,83
12,91,21,8
13,84,44,19
14,69,60,67
15,25,55,16
16,72,8,36
17,4,41,28
18,18,61,93
19,38,20,82
20,67,20,79
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim Y(20) As String
Dim X(20, 2) As Byte
For i = 1 To 20
Line Input #1, A
Y(i) = A
B = Split(A, ",")
total = 0
For j = 1 To 3
total = total + Val(B(j))
Next j
X(i, 1) = Val(B(0))
X(i, 2) = total
Next i
For i = 1 To 19
For j = (i + 1) To 20
If X(i, 2) < X(j, 2) Then
X(0, 1) = X(i, 1)
X(0, 2) = X(i, 2)
X(i, 1) = X(j, 1)
X(i, 2) = X(j, 2)
X(j, 1) = X(0, 1)
X(j, 2) = X(0, 2)
End If
Next j
Next i
For i = 1 To 20
Print #2, Y(X(i, 1)) & "," & X(i, 2)
Next i
Close #2
Close #1
End
End Sub
out.txt
11,65,90,83,238
9,98,94,29,221
14,69,60,67,196
10,90,73,28,191
6,86,65,35,186
5,25,100,58,183
18,18,61,93,172
3,76,55,39,170
20,67,20,79,166
13,84,44,19,147
19,38,20,82,140
7,13,58,57,128
12,91,21,8,120
4,12,38,69,119
16,72,8,36,116
2,18,31,64,113
8,52,12,41,105
15,25,55,16,96
17,4,41,28,73
1,9,1,20,30
佑好,
回覆刪除這程式ok, 上次提到的,如果將它放進listbox中,也可以先將不同的資料以空格分開,要取其中第幾項時,再用你們熟了的split將它分開,就沒有幾位數的問題了。