Advertisement
janac

Determine number of cars built from parts

Jan 10th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1.  
  2.  
  3. // Determine the number of cars you can
  4. // build with the parts you have.
  5. // axles, wheels, bodies
  6. //
  7. // TEST INPUT
  8. // Test One: 10 axles, 60 wheels, 17 bodies makes
  9. // 5 cars with left over: 0 axles, 40 wheels, 12 bodies
  10. // Test Two: 38 axles, 68 wheels, 25 bodies makes
  11. // 17 cars with left over: 4 axles, 0 wheels, 8 bodies
  12. // Test Three: 341 axles, 273 wheels, 67 bodies makes
  13. // 67 cars with left over: 207 axles, 5 wheels, 0 bodies
  14.  
  15. #include <iostream>
  16. #include <string>
  17.  
  18. using namespace std;
  19.  
  20. const int axles_per_car = 2; // A car has two axles.
  21. const int wheels_per_car = 4; // A car has four wheels.
  22. const int bodies_per_car = 1; // A car has one body.
  23.  
  24. // This function does two things:
  25. // 1) returns the number of complete cars that can be
  26. // built with the axles, wheels and bodies given.
  27. // 2) determines the number of leftover parts.
  28. int make_cars(int axles, int wheels, int bodies,
  29.     int& leftover_axles, int& leftover_wheels, int& leftover_bodies)
  30. {
  31.     // Determine how many cars you can build with just the axles.
  32.     int num_possible_cars_axles = axles / axles_per_car;
  33.     // Determine how many cars you can build with just the wheels.
  34.     int num_possible_cars_wheels = wheels / wheels_per_car;
  35.     // Determine how many cars you can build with just the bodies.
  36.     int num_possible_cars_bodies = bodies / bodies_per_car;
  37.  
  38.     // Assume we have enough parts to use all the axles.
  39.     // We could build half as many cars as we have axles
  40.     // 2 axles per car
  41.     int max_num_cars = num_possible_cars_axles;
  42.     // If we don't have enough wheels to build this many cars
  43.     // Update the number of cars we can build with the wheels.
  44.     if (num_possible_cars_wheels < max_num_cars)
  45.     {
  46.         max_num_cars = num_possible_cars_wheels;
  47.     }
  48.     // If we don't have enough bodies to build this many cars
  49.     // Update the number of cars we can build with the bodies.
  50.     if (num_possible_cars_bodies < max_num_cars)
  51.     {
  52.         max_num_cars = num_possible_cars_bodies;
  53.     }
  54.  
  55.     // Calculate how many leftover axles, wheels and bodies we have.
  56.     leftover_axles = axles - (axles_per_car * max_num_cars);
  57.     leftover_wheels = wheels - (wheels_per_car * max_num_cars);
  58.     leftover_bodies = bodies - (bodies_per_car * max_num_cars);
  59.  
  60.     return max_num_cars; // Return the number of cars we can build.
  61. }
  62.  
  63. int main()
  64. {
  65.    
  66.     string num_axles_string; // User input axles.
  67.     string num_wheels_string; // User input wheels.
  68.     string num_bodies_string; // User input bodies.
  69.     int num_axles = 0; // axles converted to integer.
  70.     int num_wheels = 0; // wheels converted to integer.
  71.     int num_bodies = 0; // bodies converted to integer.
  72.     int leftover_axles = 0; // Number axles leftover.
  73.     int leftover_wheels = 0; // Number wheels leftover.
  74.     int leftover_bodies = 0; // Number bodies leftover.
  75.     int cars_built = 0; // Number of complete cars built.
  76.  
  77.     cout << "This program will tell you how many "
  78.         "cars can you build from the parts you have.\n";
  79.  
  80.     cout << "For each car, you need :\n"
  81.         "2 axles\n"
  82.         "4 wheels\n"
  83.         "1 body\n";
  84.  
  85.     cout << "\nHow many axles do you have? ";
  86.     getline(cin, num_axles_string); // Get number of axles.
  87.  
  88.     cout << "\nHow many wheels do you have? ";
  89.     getline(cin, num_wheels_string); // Get number of wheels.
  90.  
  91.     cout << "\nHow many bodies do you have? ";
  92.     getline(cin, num_bodies_string); // Get number of bodies.
  93.  
  94.     // Convert axles, wheels and bodies strings to integers.
  95.     num_axles = stoi(num_axles_string);
  96.     num_wheels = stoi(num_wheels_string);
  97.     num_bodies = stoi(num_bodies_string);
  98.  
  99.     // Determine how many cars can be built with the parts we have.
  100.     cars_built = make_cars(num_axles, num_wheels, num_bodies,
  101.         leftover_axles, leftover_wheels, leftover_bodies);
  102.  
  103.     // Display the result.
  104.     cout << "\nYou can build " << cars_built << " cars.\n";
  105.     cout << "\nYou have left over:\n";
  106.     cout << leftover_axles << " axles\n";
  107.     cout << leftover_wheels << " wheels\n";
  108.     cout << leftover_bodies << " bodies\n";
  109.  
  110.     return 0; // End the program.
  111. }
  112.  
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement