fork(8) download
  1. Imports System
  2. Public Class Coins
  3. Public Shared Sub Main()
  4.  
  5.  
  6. Dim AmountToBePaid, ValueOfChoice, i, j, NrOfCoins, NrOfDenominations, NextChange, Subtotal As Integer
  7. Dim Solved, ValidInput As Boolean
  8. Dim Line, Payline, Returnline As String
  9. Dim Denominations(100), Choices(100) As Integer
  10. Dim Units() As String
  11.  
  12.  
  13. For i = 1 To 99
  14. Choices(i) = 0
  15. Next
  16.  
  17.  
  18. Line = Console.ReadLine()
  19.  
  20. If Line = "" Then
  21. Console.WriteLine("Enter amount to be paid, followed by the various denominations of coins and banknotes. All integer numbers, comma separated. Then rerun")
  22. End If
  23. Units = Line.Split(",")
  24.  
  25. ValidInput = True
  26. If Units.Length > 1 Then
  27. For i = 0 To Units.Length - 1
  28.  
  29. Denominations(i) = Int(Units(i))
  30. Denominations(i + Units.Length - 1) = -Denominations(i)
  31.  
  32. Next
  33. End If
  34.  
  35. If Units.Length < 2 Or ValidInput = False Then
  36. Console.WriteLine("Input not valid")
  37. Exit Sub
  38. End If
  39.  
  40. AmountToBePaid = Denominations(0)
  41. NrOfDenominations = 2 * Units.Length - 2
  42.  
  43. Solved = False
  44. NrOfCoins = 1
  45.  
  46. While Solved = False
  47. If Choices(1) = NrOfCoins Then
  48. NrOfCoins = NrOfCoins + 1
  49. For i = 1 To NrOfDenominations
  50. Choices(i) = 0
  51. Next
  52. End If
  53.  
  54. Subtotal = 0
  55. For i = 1 To NrOfDenominations
  56. If i = NrOfDenominations Then Choices(i) = NrOfCoins - Subtotal
  57.  
  58. If i = NextChange Then
  59. Choices(i) = Choices(i) + 1
  60. For j = i + 1 To NrOfDenominations
  61. Choices(j) = 0
  62. Next
  63. End If
  64. Subtotal = Subtotal + Choices(i)
  65. If Subtotal = NrOfCoins And Choices(i) <> 0 Then
  66. NextChange = i - 1
  67. End If
  68. Next
  69.  
  70. ValueOfChoice = 0
  71. For i = 1 To NrOfDenominations
  72. ValueOfChoice = ValueOfChoice + Choices(i) * Denominations(i)
  73. Next
  74. If ValueOfChoice = AmountToBePaid Then Solved = True
  75. End While
  76.  
  77. Payline = "Pay: "
  78. Returnline = "Return: "
  79. For i = 1 To NrOfDenominations
  80. If Choices(i) > 0 Then
  81. If Denominations(i) > 0 Then
  82. Payline = Payline & Str(Choices(i)) & " x " & Str(Denominations(i)) & " ; "
  83. Else
  84. Returnline = Returnline & Str(Choices(i)) & " x " & Str(-Denominations(i)) & " ; "
  85. End If
  86. End If
  87. Next
  88. Console.WriteLine(Payline)
  89. Console.WriteLine(Returnline)
  90.  
  91. End Sub
  92.  
  93. End Class
Success #stdin #stdout 0.08s 25712KB
stdin
18,1,10
stdout
Pay:  2 x  10 ;  
Return:  2 x  1 ;