Find minimum number of coins that make a given value: Difference between revisions
Find minimum number of coins that make a given value (view source)
Revision as of 08:47, 27 October 2022
, 1 year agono edit summary
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
No edit summary |
||
Line 381:
1 * 2
1 * 1</pre>
=={{header|FutureBasic}}==
Task solution wrapped into a general purpose function with test examples shown.
<syntaxhighlight lang="futurebasic">
void local fn MinimumCoinsForValue( value as NSUInteger, coins as CFArrayRef )
NSUInteger i, count, tmp
CFStringRef coinStr = fn ArrayComponentsJoinedByString( coins, @", " )
printf @"The minimum number of coins valued %@ needed to total %lu is:", coinStr, value
count = len(coins)
for i = count to 1 step -1
tmp = (NSUInteger)fn floor( value / fn NumberIntegerValue( coins[i-1] ) )
if ( tmp > 0 )
printf @"%lu * %@", tmp, coins[i-1]
value = value mod fn NumberIntegerValue( coins[i-1] )
end if
next
end fn
fn MinimumCoinsForValue( 988, @[@1, @2, @5, @10, @20, @50, @100, @200] )
print : print
fn MinimumCoinsForValue( 1024, @[@1, @2, @5, @10, @20, @50, @100, @200] )
print : print
fn MinimumCoinsForValue( 65273, @[@1, @2, @5, @10, @20, @50, @100, @200] )
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
The minimum number of coins valued 1, 2, 5, 10, 20, 50, 100, 200 needed to total 988 is:
4 * 200
1 * 100
1 * 50
1 * 20
1 * 10
1 * 5
1 * 2
1 * 1
The minimum number of coins valued 1, 2, 5, 10, 20, 50, 100, 200 needed to total 1024 is:
5 * 200
1 * 20
2 * 2
The minimum number of coins valued 1, 2, 5, 10, 20, 50, 100, 200 needed to total 65273 is:
326 * 200
1 * 50
1 * 20
1 * 2
1 * 1
</pre>
|