代码之家  ›  专栏  ›  技术社区  ›  StealthRT

帮助找回零钱($1、$5、$10、$20、Q、D、N和便士)

  •  -1
  • StealthRT  · 技术社区  · 15 年前

    嘿,各位,有谁能帮我做这个功能的美元。它做的改变很好,但它确实 我想要大钞($1,$5,$10,&$20) 是美元的,还有Q/D/N/P的变化。

    Dim Quarters As Integer
    Dim Dimes As Integer
    Dim Nickels As Integer
    Dim Pennies As Integer
    
    Sub GetChange(ByVal Amount As Currency, ByRef Quarters As Integer, ByRef Dimes As Integer, ByRef Nickels As Integer, ByRef Pennies As Integer)
    Dim Cents As Integer
    
       Cents = Amount * 100
       Quarters = Cents \ 25
       Cents = Cents Mod 25
       Dimes = Cents \ 10
       Cents = Cents Mod 10
       Nickels = Cents \ 5
       Pennies = Cents Mod 5
    
    End Sub
    
    Call GetChange(5.56, Quarters, Dimes, Nickels, Pennies)
    

    更新,已解决

    Private Sub theUSChange(Amount)
        Dim USCurrency(9) As Currency
        Dim USCurrencyNames(9) As Currency
        Dim Amount As Currency
        Dim Result As Currency
        Dim I As Integer
    
        USCurrencyNames(0) = " Pennies: "
        USCurrency(0) = 0.01
        USCurrencyNames(1) = "   Dimes: "
        USCurrency(1) = 0.05
        USCurrencyNames(2) = " Nickles: "
        USCurrency(2) = 0.1
        USCurrencyNames(3) = "Quarters: "
        USCurrency(3) = 0.25
    
        USCurrencyNames(4) = "      $1: "
        USCurrency(4) = 1
        USCurrencyNames(5) = "      $5: "
        USCurrency(5) = 5
        USCurrencyNames(6) = "     $10: "
        USCurrency(6) = 10
        USCurrencyNames(7) = "     $20: "
        USCurrency(7) = 20
        USCurrencyNames(8) = "     $50: "
        USCurrency(8) = 50
        USCurrencyNames(9) = "    $100: "
        USCurrency(9) = 100
    
        For I = UBound(USCurrency) To LBound(USCurrency) Step -1
            Do While Amount >= USCurrency(I)
                Amount = Amount - USCurrency(I)
                Result = Result + 1
            Loop
            Debug.Print(USCurrencyNames(I) & Result)
            Result = 0
        Next
    End Sub
    
    call theUSChange(5.77)
    
    OUTPUT:
        $100: 0
         $50: 0
         $20: 0
         $10: 0
          $5: 1
          $1: 0
    Quarters: 3
     Nickles: 0
       Dimes: 0
     Pennies: 2
    

    大卫

    2 回复  |  直到 15 年前
        1
  •  0
  •   John D. Cook    15 年前

    这个问题是作为一个例子来解决的 Concrete Mathematics 使用生成函数。