brute-force search program with C++ -
i new c++. going through tutorial google developer: https://developers.google.com/edu/c++/getting-started
here simple match puzzle brute force search solution: horses cost $10, pigs cost $3, , rabbits $0.50. farmer buys 100 animals $100, how many of each animal did buy?
here code:
#include <iostream> using namespace std; int main() { int phorse = 10; int ppig = 3; int prabbit = 0.5; (int = 0; <= 100 / phorse; ++i) { (int j = 0; j <= ((100 - * phorse) / ppig); ++j) { int money = (100 - phorse * - ppig * j); if (prabbit * (100 - - j) == money) { cout << "the number of horses are: " << << endl; cout << "the number of pigs are: " << j << endl; cout << "the number of rabbits are: " << 100 - - j << endl; } } } return 0; }
however, giving me ridiculous answers [10 0 90], not correct obviously.
i not figure out problem. idea? in advance.
instead of
int prabbit = 0.5;
try
double prabbit = 0.5;
Comments
Post a Comment