2012年11月24日 星期六

99模擬 Problem 4:排列組合的應用


Problem 4:排列組合的應用

子題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

2 則留言:

  1. Dim strr, moneyy
    Dim listt(24) As String
    Dim money(24), meat, vege, egg, fruit, mycount As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    mycount = 0
    Input #1, meat, vege, egg, fruit
    Line Input #1, moneyy
    moneyy = " , " & moneyy
    moneyy = Split(moneyy, ", ")
    strr = "肉 菜 蛋 果"
    strr = Split(strr, " ")
    Call rr("")
    For i = 0 To List1.ListCount - 1
    For ii = 1 To 24
    If List1.List(i) + 100000 = money(ii) Then Print #2, listt(ii) & " " & money(ii): Exit For
    Next
    Next
    Close #2
    Close #1
    End
    End Sub

    Public Function rr(k As String)
    If Len(k) = 4 Then
    mycount = mycount + 1
    listt(mycount) = k
    For ii = 1 To 4
    If Mid(k, ii, 1) = "肉" Then money(mycount) = money(mycount) + meat * moneyy(ii)
    If Mid(k, ii, 1) = "菜" Then money(mycount) = money(mycount) + vege * moneyy(ii)
    If Mid(k, ii, 1) = "蛋" Then money(mycount) = money(mycount) + egg * moneyy(ii)
    If Mid(k, ii, 1) = "果" Then money(mycount) = money(mycount) + fruit * moneyy(ii)
    Next
    List1.AddItem money(mycount) - 100000
    Else
    For i = 0 To UBound(strr)
    If InStr(k, strr(i)) = 0 Then Call rr(k & strr(i))
    Next
    End If
    End Function

    回覆刪除
  2. Dim ve()
    Dim m() As String
    Dim kg() As String
    Dim money(), r As Integer
    Dim vname() 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
    Line Input #1, y
    ve() = Array("肉", "菜", "蛋", "果")
    m = Split(x, ",")
    kg = Split(y, ",")
    r = 1
    Call a("", 0)
    For i = 1 To UBound(money)
    For j = i + 1 To UBound(money)
    If money(i) < money(j) Then
    op = money(i): money(i) = money(j): money(j) = op
    ip = vname(i): vname(i) = vname(j): vname(j) = ip
    End If
    Next j
    Next i
    For i = 1 To UBound(money)
    Print #2, vname(i) & " " & money(i)
    Next i
    Close #2
    Close #1
    End
    End Sub
    Function a(b, c)
    If Len(b) = 4 Then
    ReDim Preserve vname(r), money(r)
    vname(r) = b: money(r) = c: r = r + 1
    Else
    For i = 0 To UBound(ve)
    If InStr(b, ve(i)) = 0 Then Call a(b & ve(i), c + Val(kg(Len(b))) * Val(m(i)))
    Next i
    End If
    End Function

    回覆刪除