How to resolve the algorithm Count the coins step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Count the coins step by step in the Delphi programming language

Table of Contents

Problem Statement

There are four types of common coins in   US   currency:

There are six ways to make change for 15 cents:

How many ways are there to make change for a dollar using these common coins?     (1 dollar = 100 cents).

Less common are dollar coins (100 cents);   and very rare are half dollars (50 cents).   With the addition of these two coins, how many ways are there to make change for $1000? (Note:   the answer is larger than   232).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Count the coins step by step in the Delphi programming language

Source code in the delphi programming language

program Count_the_coins;

{$APPTYPE CONSOLE}

function Count(c: array of Integer; m, n: Integer): Integer;
var
  table: array of Integer;
  i, j: Integer;
begin
  SetLength(table, n + 1);
  table[0] := 1;
  for i := 0 to m - 1 do
    for j := c[i] to n do
      table[j] := table[j] + table[j - c[i]];
  Exit(table[n]);
end;

var
  c: array of Integer;
  m, n: Integer;

begin
  c := [1, 5, 10, 25];

  m := Length(c);
  n := 100;
  Writeln(Count(c, m, n));  //242
  Readln;
end.


  

You may also check:How to resolve the algorithm Loops/Continue step by step in the Spin programming language
You may also check:How to resolve the algorithm Strip block comments step by step in the Delphi programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Arturo programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the Phix programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Draco programming language