Designating, respectively, the numPatos
, numCoelhos
, numPes
and numCabecas
the numbers of ducks, rabbits, feet and heads, it is easy to see that
2*numPatos + 4*numCoelhos = numPes
and that
numPatos + numCoelhos = numCabecas
.
Solving this system of linear equations, we will arrive at the following solution:
numPatos = 2*numCabecas-numPes/2
numCoelhos = -numCabecas+numPes/2
Now just implement the solution found:
#include <iostream>
using namespace std;
int main(){
unsigned int numPatos, numCoelhos, numPes, numCabecas;
cout << "Numero de pes: ";
cin >> numPes;
cout << "Numero de cabecas: ";
cin >> numCabecas;
numPatos = 2*numCabecas-numPes/2;
numCoelhos = -numCabecas+numPes/2;
cout << endl
<< "Tem na sua cerca " << numPatos << " pato(s) e " << numCoelhos << " coelho(s).";
return 0;
}
In a quick google search, I found several answers to this problem. If you have already implemented could put the code to analyze.
– Skywalker