Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dogs, Cats and Mice problem

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,502
US

Here is one that I love. My co-workers were trying to solve it while driving home. Very dangerous. And it is perfect if you want to teach loops in computer programming.

You're given a hundred dollars and told to spend it all purchasing exactly a hundred animals at the pet store. Dogs cost $15. Cats cost a buck, and mice are 25 cents each. And you have to purchase at least one of each animal.

How many Dogs, Cats and Mice do you need to buy?

Have fun.

---- Andy
 
3 dogs, 41 cats, and 56 mice
 
There are multiple solutions. In addition to the above: 6 dogs, 9 cats, 4 mice would work for a minimal solution.

 
Noway2,
Your solution does not add up to 100 animals.
 
[hide]Without using loops:
You have three unknowns and two equations:
x + y + z = 100
15x + y + 0.25z = 100
What you do know is that 1 <= x <= 6, so you only have six possible solutions, each which can be checked with two simultaneous equations:
for x = 1: y + z = 99 and y + 0.25z = 85
for x = 2: y + z = 98 and y + 0.25z = 70
for x = 3: y + z = 97 and y + 0.25z = 55
for x = 4: y + z = 96 and y + 0.25z = 40
for x = 5: y + z = 95 and y + 0.25z = 25
for x = 6: y + z = 94 and y + 0.25z = 10
But, before you do that, you also know that mod(z, 4) = 0, so y + z must be a multiple of 4 plus 1. That eliminates options except for x = 3.
Therefore x = 3, y + z = 97 and y + 0.25z = 55
Solving simultaneously, we get y = 97 - z, so
97 - z + .25z = 55
97 - .75z = 55
.75x = 42
z = 56

y + 56 = 97
y = 41

x = 3 dogs
y = 41 cats
z = 56 mice
[/hide]

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I got Jgres solution and agree that it is the only solution.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Excellent explanation by CajunCenturion.
 
another way to look at it without loops:
Using the same two equations as Cajun used
(1) 15d+c+(m/4)=100
(2) 15d+15c+15m=100*15
subtract (1) from (2)
14c+(59/4)m=1400
therefore c+(59/56)m=100
Obviously both (59/56)m and m itself must be integers. m is an integer by definition, and (59/56)m must be because otherwise c won't be.
The smallest (and only) solution is m=56, so (59/56)m=59
Therefore c=41
And since the animals add up to 100, d=3
 
The not so clever brute force solution of loops has one advantage over all analytical solutions. You can much easier introduce other numbers, eg prices and find their solution too.

Code:
#include <stdio.h>
#include <iostream>

#define CAPITAL 200
#define NUMBEROFANIMALS 200

#define DOGPRICE 15
#define CATPRICE 1
#define MOUSEPRICE .25

int main(int argc, char **argv)
{
 int Dogs,Cats,Mice;

 for (Dogs = 1;Dogs <= int (CAPITAL/DOGPRICE); Dogs++)
   for (Cats = 1;Cats <= int ((CAPITAL-DOGPRICE*Dogs)/CATPRICE); Cats++)
      {
       Mice = NUMBEROFANIMALS-Cats-Dogs;
      if (Mice>0 && DOGPRICE*Dogs+CATPRICE*Cats+MOUSEPRICE*Mice == CAPITAL)
         cout << "Solution:", Dogs, Cats, Mice;
      }
 return 0;
}

Here for example both the capital and the number of animals wanted is doubled to 200. There are 3 solutions for that.

Bye, Olaf.
 
Prolog ise usefull for this kind of game :

Code:
:- use_module(library(clpfd)).

dogs_cats_mices(D, C, M) :-
	S = [D, C, M],
	S ins 1..100,
	% Number of animals
	sum(S, #=, 100),
	% Price
	% with clpfd, we can only use integers
	(D * 1500 + C * 100 + M * 25) #= 10000,
	labeling([], S).
And we get :
?- dogs_cats_mices(Dogs, Cats, Mices).
Dogs = 3,
Cats = 41,
Mices = 56 ;
false.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top