puzle [1980] : change for a euro

General FreeBASIC programming questions.
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

puzle [1980] : change for a euro

Postby bluatigro » Apr 13, 2020 6:54

puzle :
give all different changes in cions for a euro


this is my try :

Code: Select all

dim shared as integer coin(5) = { 1 , 2 , 5 , 10 , 20 , 50 }
sub change( high as integer , rest as integer )
  if high < 0 then exit sub
  dim as integer a = int( rest / coin( high ) ) , i
  for i = a to 1 step -1
    rest = a * coin( high ) - i * coin( high )
    print "(" ; coin( high ) ; ") * " ; i ; "   " ;
    change high - 1 , rest
    print
  next i
end sub

change 5 , 100

print "[ push return ]"
sleep


error :
code does not give al coins in a try
not al try's are printed
fxm
Posts: 9947
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: puzle [1980] : change for a euro

Postby fxm » Apr 13, 2020 14:09

I think that such a recursive code kind is rather adapted to solve the problem of finding the optimized combination of coins to pay a certain amount (instead of all existing combinations):

Code: Select all

dim shared as integer coin(5) = {1 , 2 , 5 , 10 , 20 , 50}

sub change (byval high as integer, byval rest as integer)
    if high < 0 then print : exit sub
    dim as integer a = rest \ coin(high)
    if a > 0 then
        print "[" & coin(high) & "] * " & a & "   ";
    end if
    change high - 1 , rest - a * coin(high)
end sub

change 5 , 47

sleep
Muttonhead
Posts: 130
Joined: May 28, 2009 20:07

Re: puzle [1980] : change for a euro

Postby Muttonhead » Apr 13, 2020 16:19

oops, I guess that didn't quite work out
Mutton
Last edited by Muttonhead on Apr 13, 2020 20:09, edited 1 time in total.
dodicat
Posts: 6688
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: puzle [1980] : change for a euro

Postby dodicat » Apr 13, 2020 17:18

Pounds over here (Except Southern Ireland)
Each country has it's own notes/coins
Scotland, England, N.Ireland, Isle of Man, Channel Islands.
There are some oddballs like Rockall and some river forts on the Thames e.t.c. , but these are just by cranks.
I suppose the same thing happens in other parts of Europe.

Code: Select all


'change due in array s()
Sub getchange(m As Long,s() As Long)
  Static As Long c(1 To 12)={5000,2000,1000,500,200,100,50,20,10,5,2,1} 'pennies
  Redim s(0)
  For n As Long=1 To 12
    If m>=c(n) Then
      Do
        Redim Preserve s(Ubound(s)+1)
        s(Ubound(s))=c(n)
        m=m-c(n)
      Loop Until m<c(n)
    End If
  Next n
End Sub

Do
  Cls
  Locate 3
  Redim As Long s()
  Dim As Long p=Rnd*10000
  Dim As Single tot
  Print Using "Change in Pounds ##.##"; p/100
  Print
  getchange(p,s())
 
  For n As Long=1 To Ubound(s)
    If s(n)/100<1 Then
      Print s(n)/100,"(";str(s(n));" pence)"
    Else
      Print s(n)/100
    End If
   
    tot+=s(n)/100
  Next
  Print "-----------"
  Print Using "##.##";  tot
  Sleep
  Loop Until Inkey=Chr(27) 

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests