C++ code to find the number of refill packs to be bought


Suppose, there are 'a' number of matches and 'b' number of press conferences to be held in a stadium in a particular week. There are two cafeterias, one in the player dressing room and one in the press conference area. There are two soft drink dispensers in the cafeterias and they need to be filled at the start of the week. The dressing room cafeteria drink dispenser is used heavily, and it needs to be refilled after every 'c' games and the conference area cafeteria's dispenser needs to be refilled after every 'd' events. The stadium maintenance committee can order k drink refill packs at the beginning of each week, 'x' packs for the dressing room cafeteria, and 'y' packs for the conference room cafeteria where x + y <= k. Given the match schedule, we have to find out the values of x and y, and if more than k refill packs are needed, we print "Limit Exceeded".

So, if the input is like a = 8, b = 8, c = 4, d = 6, k = 9, then the output will be 2, 2.

Steps

To solve this, we will follow these steps −

a := (c + a - 1) / c
b := (d + b - 1) / d
if a + b <= k, then:
   print(a, b)
Otherwise,
   print("Limit Exceeded")

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int a, int b, int c, int d, int k) {
   a = (c + a - 1) / c;
   b = (d + b - 1) / d;
   if(a + b <= k)
      cout<<a<<" "<<b<<"\n";
   else
      cout<<"Limit Exceeded."<<"\n";
}
int main() {
   int a = 8, b = 8, c = 4, d = 6, k = 9;
   solve(a, b, c, d, k);
   return 0;
}

Input

8, 8, 4, 6, 9

Output

2 2

Updated on: 29-Mar-2022

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements