Using the Minimum coins to arrive at a total using Perl

Input Sample:

Your program should accept as its first argument a path to a filename. Each line in this file contains a positive integer number which represents the total you have to arrive at e.g.

11
20

Script:
open(INFILE, "$ARGV[0]") or die("Cannot open file $ARGV[0] for reading: $!");
 while(my $line = <INFILE>) 
{ chomp($line); 
my $num = $line; 
my $snum; 
my $sum = 0; 
$sum = $sum + int($num/5); 
$num = $num%5;
 $sum = $sum + int($num/3); 
$num = $num%3; 
$sum = $sum + int($num/1); 
$num = $num%1;
 print "$sum\n";
 } 
close(INFILE);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s