Does anybody know how to do this programm?

Atomicman

Banned
Sep 23, 2000
130
0
0
The shipping clerk at the Dewey Flapper company is faced with the following problem: Dewey Flappers are very delicate and must be shipped in special containers. These containers come in four sizes, huge, large, medium, and small, which can hold 50, 20, 5 and 1 Dewey Flappers, respectively. Write a program that reads (from the keyboard) the number of Flappers to be shipped and prints the number of huge, large, medium, and small containers needed to send the shipment in the minimum number of containers and whith the minimum amount of wasted space. Use the % operator in your program. Use named constants for the number of Flappers each type of container can hold. The output should be similar to the following:



Container
Number

=======
======

Huge
21

Large
2

Medium
1

Small
3


Execute the program for 3, 18, 48, 78, and 10,598 Flappers. Hand in a listing of your program together with output redirected to a file.

Pay attention to appropriate comments, meaningful variable names and readable format.

 

YaKuZa

Senior member
Aug 26, 2000
995
0
0
um, which language do u have to write it in? oh well, doesnt really matter since im not the one writing the program. anyway, just store the user input and u can use a series of if statements to compare the input with each container size. once thats done, just use the mod command to see how many flappers are left over and compare it again. putting the entire program in a while loop should work.
 

br0wn

Senior member
Jun 22, 2000
572
0
0
dynamic programming problem.

edit : Yakuza, the problem is not that
simple. The KEY is that it asked for MINIMUM containers and
minimum waste space. Your method is using
greedy algorithm (but you still have to start from
largest container), works fine except it doesn't
guarantee OPTIMAL solution.
Dynamic programming will solve this problem easily.

But Atomicman, aren't you supposed to
stockpile for gas and oil and build shelters ?
read here
You won't have time to finish this program, you
still have to build bunkers and stockpile large amount of oil, gas, water, food, and don't forget air :p.
 

Agaemmon

Member
Aug 7, 2000
33
0
0
Ok, I have not taken any programming classes, and being entirely self taught, I decided to take a stab at this. I wrote this in perl and as far as I can see it will work for any number of flappers. It is not as modifiable as possible because I wanted to write it quickly.


#!/usr/local/bin/perl

#--Must sort into containers --
#--Huge- 50 items
#--Large- 20 items
#--Medium- 5 items
#--Small- 1 items

sub main {
my $dewies = 0;
print "Please enter the number of flappers you need containers for: ";
$dewies = <>;
chomp $dewies;
&amp;sortflaps($dewies);
}

sub sortflaps {
my $flaps = $_[0];
if($flaps < 1) {
print &quot;You did not enter a valid number of flappers. Try again.\n&quot;;
exit 0;
}
&amp;hugecont($flaps);
}

sub hugecont {
my $number = $_[0];
if($number >= 50) {
my ($containers, $left) = &amp;workcont(50,$number);
print &quot;Huge = $containers\n&quot;;
&amp;largecont($left);
}
else {
print &quot;Huge = 0\n&quot;;
&amp;largecont($number);
}
return 1;
}

sub largecont{
my $number = $_[0];
if($number >= 20) {
my ($containers, $left) = &amp;workcont(20,$number);
print &quot;Large = $containers\n&quot;;
&amp;mediumcont($left);
}
else {
print &quot;Large = 0\n&quot;;
&amp;mediumcont($number);
}
return 1;
}

sub mediumcont {
my $number = $_[0];
if($number >= 5) {
my ($containers, $left) = &amp;workcont(5,$number);
print &quot;Medium = $containers\n&quot;;
&amp;smallcont($left);
}
else {
print &quot;Medium = 0\n&quot;;
&amp;smallcont($number);
}

return 1;
}

sub smallcont {
my $number = $_[0];
if($number >= 1) {
my ($containers, $left) = &amp;workcont(1,$number);
print &quot;Small = $containers\n&quot;;
}
else {
print &quot;Small = 0\n&quot;;
}
return 1;
}

sub workcont {
my $divider = $_[0];
my $num = $_[1];
my $remainder = $num % $divider;
my @returns = &quot;&quot;;
if ($remainder > 0) {
$num = $num - $remainder;
}
$returns[0] = $num / $divider;
$returns[1] = $remainder;
return @returns;
}

&amp;main();


Please reply with your comments on my code.
 

Agaemmon

Member
Aug 7, 2000
33
0
0
The reply took all of the extra spaces used in formatting for my code. Just wanted you to know I don't write code formatted like that.
 

Agaemmon

Member
Aug 7, 2000
33
0
0
Here is another way to do this program. This one is much more flexible, allowing you to enter the sizes of the containers you wish to use, but will use the default if you want. Also much more concise code.

#!/usr/local/bin/perl

#--Must sort into containers --
#--Huge- 50 items
#--Large- 20 items
#--Medium- 5 items
#--Small- 1 items

sub main {
my $dewies = 0;
@containers = &quot;&quot;;
my $sizes = &quot;&quot;;
print &quot;Enter the sizes of the containers seperated by spaces(to use default press enter): &quot;;
$sizes = <>;
chomp $sizes;
if($sizes ne &quot;&quot;) {
@containers = split(/ /, $sizes);
}
else {
@containers = (50, 20, 5, 1);
}
print &quot;Please enter the number of flappers you need containers for: &quot;;
$dewies = <>;
chomp $dewies;
&amp;sortflaps($dewies, @containers);
}

sub sortflaps {
my ($flaps, @containers) = @_;
if($flaps < 1) {
print &quot;You did not enter a valid number of flappers. Try again.\n&quot;;
exit 0;
}
@containers = &amp;checkdupes(@containers);
@containers = sort {$b<=>$a} (@containers);
foreach $size (@containers) {
$flaps = &amp;workcont($size,$flaps);
}
}

sub workcont {
my $divider = $_[0];
my $num = $_[1];
my $remainder = $num % $divider;
my @returns = &quot;&quot;;
if ($remainder > 0) {
$num = $num - $remainder;
}
print &quot;Number of $divider Size containers: &quot;;
print $num / $divider;
print &quot;\n&quot;;
return $remainder;
}

sub checkdupes {
my @check = @_;
my %copy = &quot;&quot;;
my $x = 0;

for ($x = 0; $x < $#check + 1; $x++) {
if($copy{$check[$x]} == 1) {
delete $check[$x];
}
$copy{$check[$x]} = 1;
}
return @check;
}

&amp;main();
 

br0wn

Senior member
Jun 22, 2000
572
0
0
Agaemmon,
Your first program is ok, but the second one is not quite correct
since you are allowing the user to enter the size of the containers.

Consider this case :
Size of containers entered by user : 1, 4, and 6.

Now supposed we want to ship 8 flappers.

Using your program it will take ONE of size 6 container,
and TWO of size 1 container, totalling 3 containers.

It is obvious the minimum number of containers needed
are 2, (2 of size 4 container).


edit : As I've said b4 this is quite tricky for some cases.
(although that algorithm works fine for 50, 20, 5, and 1).

 

Agaemmon

Member
Aug 7, 2000
33
0
0
No, because in his description he said he wanted the least wasted space possible, which means no wasted space, which is the way I have done it.
 

br0wn

Senior member
Jun 22, 2000
572
0
0
its all fun :)
I'm just telling you that this is one of the famous ALGORITHM
problem.



edit : btw the least wasted space is obvious, since he
has container that can hold 1 flapper, thus you can have
NO wasted space at all given any number of flappers.