Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Determine the number of cars you can
- // build with the parts you have.
- // axles, wheels, bodies
- //
- // TEST INPUT
- // Test One: 10 axles, 60 wheels, 17 bodies makes
- // 5 cars with left over: 0 axles, 40 wheels, 12 bodies
- // Test Two: 38 axles, 68 wheels, 25 bodies makes
- // 17 cars with left over: 4 axles, 0 wheels, 8 bodies
- // Test Three: 341 axles, 273 wheels, 67 bodies makes
- // 67 cars with left over: 207 axles, 5 wheels, 0 bodies
- #include <iostream>
- #include <string>
- using namespace std;
- const int axles_per_car = 2; // A car has two axles.
- const int wheels_per_car = 4; // A car has four wheels.
- const int bodies_per_car = 1; // A car has one body.
- // This function does two things:
- // 1) returns the number of complete cars that can be
- // built with the axles, wheels and bodies given.
- // 2) determines the number of leftover parts.
- int make_cars(int axles, int wheels, int bodies,
- int& leftover_axles, int& leftover_wheels, int& leftover_bodies)
- {
- // Determine how many cars you can build with just the axles.
- int num_possible_cars_axles = axles / axles_per_car;
- // Determine how many cars you can build with just the wheels.
- int num_possible_cars_wheels = wheels / wheels_per_car;
- // Determine how many cars you can build with just the bodies.
- int num_possible_cars_bodies = bodies / bodies_per_car;
- // Assume we have enough parts to use all the axles.
- // We could build half as many cars as we have axles
- // 2 axles per car
- int max_num_cars = num_possible_cars_axles;
- // If we don't have enough wheels to build this many cars
- // Update the number of cars we can build with the wheels.
- if (num_possible_cars_wheels < max_num_cars)
- {
- max_num_cars = num_possible_cars_wheels;
- }
- // If we don't have enough bodies to build this many cars
- // Update the number of cars we can build with the bodies.
- if (num_possible_cars_bodies < max_num_cars)
- {
- max_num_cars = num_possible_cars_bodies;
- }
- // Calculate how many leftover axles, wheels and bodies we have.
- leftover_axles = axles - (axles_per_car * max_num_cars);
- leftover_wheels = wheels - (wheels_per_car * max_num_cars);
- leftover_bodies = bodies - (bodies_per_car * max_num_cars);
- return max_num_cars; // Return the number of cars we can build.
- }
- int main()
- {
- string num_axles_string; // User input axles.
- string num_wheels_string; // User input wheels.
- string num_bodies_string; // User input bodies.
- int num_axles = 0; // axles converted to integer.
- int num_wheels = 0; // wheels converted to integer.
- int num_bodies = 0; // bodies converted to integer.
- int leftover_axles = 0; // Number axles leftover.
- int leftover_wheels = 0; // Number wheels leftover.
- int leftover_bodies = 0; // Number bodies leftover.
- int cars_built = 0; // Number of complete cars built.
- cout << "This program will tell you how many "
- "cars can you build from the parts you have.\n";
- cout << "For each car, you need :\n"
- "2 axles\n"
- "4 wheels\n"
- "1 body\n";
- cout << "\nHow many axles do you have? ";
- getline(cin, num_axles_string); // Get number of axles.
- cout << "\nHow many wheels do you have? ";
- getline(cin, num_wheels_string); // Get number of wheels.
- cout << "\nHow many bodies do you have? ";
- getline(cin, num_bodies_string); // Get number of bodies.
- // Convert axles, wheels and bodies strings to integers.
- num_axles = stoi(num_axles_string);
- num_wheels = stoi(num_wheels_string);
- num_bodies = stoi(num_bodies_string);
- // Determine how many cars can be built with the parts we have.
- cars_built = make_cars(num_axles, num_wheels, num_bodies,
- leftover_axles, leftover_wheels, leftover_bodies);
- // Display the result.
- cout << "\nYou can build " << cars_built << " cars.\n";
- cout << "\nYou have left over:\n";
- cout << leftover_axles << " axles\n";
- cout << leftover_wheels << " wheels\n";
- cout << leftover_bodies << " bodies\n";
- return 0; // End the program.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement