Code Comments
Programming Forum and web based access to our favorite programming groups.Hello! I want to solve the following problem with Fortran: I want to compute the number of combinations for builing a concret amount of money with coins, e.g. 5 Euros with 2/1/0.5/0.2/0.1/0.05/0.02/0.01 coins. Is there a simple way to do that? With thanks Yours Gernot
Post Follow-up to this messageBelow I use 0.01 Euros as the unit, so that all coins are integer-valued. The output: D:\test>changeways 6295434 -------------- changeways.f90 -------------- program test print *,changeways((/1,2,5,10,20,50,100,200/), 500) contains recursive integer function changeways(coinlist, amount) result(c) integer, intent(in) :: coinlist(:), amount integer :: coin, n, i coin = coinlist(1) if (amount == 0) then c = 1 elseif (size(coinlist) == 1) then if (mod(amount, coin) == 0) then c = 1 else c = 0 endif else n = amount/coin c = 0 do i=0,n c = c + changeways(coinlist(2:), amount-i*coin) enddo endif end function changeways end program test ----------- end changeways.f90 ------------ "Gernot Pfanner" <pfannerg@stud.uni-graz.at> wrote in message news:41850e93$0$31856$91cee783@newsreade r01.highway.telekom.at... > Hello! > > I want to solve the following problem with Fortran: > > I want to compute the number of combinations for builing a concret amount of > money with coins, e.g. 5 Euros with 2/1/0.5/0.2/0.1/0.05/0.02/0.01 coins. > > Is there a simple way to do that? > > With thanks > > Yours Gernot
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.