#!/usr/bin/perl use warnings; use strict; use payment; my @my_bills = qw (200 100 50 20 10 5 2 1); my @my_payments = qw{135 80 66 150 69}; my $mp = payment->new(); $mp->change(@my_bills); foreach (@my_payments) { $mp->add($_); } my %result = $mp->get(); foreach ( sort { $b <=> $a } keys(%result) ) { print "$_ \t: $result{$_}\n"; } #### package payment; use warnings; use strict; sub new { my $class = shift; my $self = {}; $self->{units} = undef; $self->{bills} = undef; bless $self, $class; } sub change { my $self = shift; @{ $self->{units} } = @_; %{ $self->{bills} } = map { $_ => 0 } @_; } sub add { my $self = shift; my @units = @{ $self->{units} }; my %pieces = %{ $self->{bills} }; my $temp = shift; my $unit; my $i = 0; while ($temp) { $unit = $units[$i]; while ( $temp >= $unit ) { $temp -= $unit; $pieces{$unit} += 1; } last if $i == (@units); $unit = $units[ ++$i ]; } %{ $self->{bills} } = %pieces; } sub get { my $self = shift; return %{ $self->{bills}}; } 1;