- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximize big when both big and small can be exchanged in C++
We are given a big object let’s say, ‘a’ and a small object let’s say, ‘b’. The choices for the object ‘a’ and ‘b’ depend upon the user. In the example below, we are taking objects to be toys which are big as well as small as per the size characteristics. The task is to calculate the maximum number of big toys that can be achieved by giving the small toys in return.
Input − big_toys = 8, small_toys = 20, a = 6, b = 4
Output − Maximize big when both big and small can be exchanged are − 11
Explanation − Maximum number of big toys that can be bought using small toys i.e. 20 are 11 in number
Input − big_toys = 3, small_toys = 10, a = 4, b = 2
Output − Maximize big when both big and small can be exchanged are: 5
Explanation − Maximum number of big toys that can be bought using small toys i.e. 10 are 5 in number
Approach used in the below program is as follows
Input the total number of big toys and small toys. Also, take ‘a’ as the total of big toys that can be exchanged in return of small toys and ‘b’ as the total of small toys that can be exchanged in return of big toys.
If a < b then set small toys to be the sum of total number of small toys available with the b * total number of big toys available and set count of big toys as 0.
Now, set big toys to be the sum of the total number of big toys available with the small toys divided by a.
Return the total of big toys as we need the maximum of big toys that can be exchanged in return of small toys.
Print the result.
Example
#include <iostream> using namespace std; int maximum(int big_toys, int small_toys,int a, int b){ if (a < b){ small_toys += b * big_toys; big_toys = 0; } big_toys += (small_toys / a); return big_toys; } int main(){ int big_toys = 8, small_toys = 20; int a = 6, b = 4; cout<<"Maximize big when both big and small can be exchanged are:"<<maximum(big_toys, small_toys, a, b); return 0; }
Output
Maximize big when both big and small can be exchanged are: 11