In: Computer Science
Write it in C++
The most disgusting recursive problem ever. The McDonald’s near campus sells Chicken McNuggets in orders of 6, 9, or 20. Suppose you are ordering for a party and you know exactly how many McNuggets will be eaten by guests. It turns out that, for any integer n ≥ 44, you can order exactly n Chicken McNuggets at this McDonald’s. For purposes of this problem, you cannot throw out McNuggets or allow them to go uneaten, such as by acquiring n = 44 by buying two twenty packs and a six pack, then discarding two. If the thought of this many Chicken McNuggets is too disgusting, you may pretend you are buying n ≥ 44 celery sticks in bunches of 6, 9, or 20 (feel free to rename the function below in that case). Finish the recursive function below to complete the ordering and return the counts by reference parameters. You may assume for this problem that there will be no overflow or underflow at any point in the problem and that stack space is not a concern. The code has been started for you and is part of a correct solution.
void buyChicken ( unsi gned n , un si gned & num6Packs , un si gned & num9Packs , un signed & num20Packs ) { i f ( 44 == n ) { num20Packs = 1 ; num6Packs = 4 ; num9Packs = 0 ; }
e l s e i f ( 4 5 == n ) { num20Packs = 0 ; num6Packs = 3 ; num9Packs = 3 ; } e l s e i f ( 4 6 == n ) { num20Packs = 2 ; num6Packs = 1 ; num9Packs = 0 ; }
Solution:
If n is divisible by 6, just buy all the num6packs.
else If n is divisible by 9, just buy all the num9packs.
else if n is divisible by 3, buy 1 num9pack and now n will be divisible by 6, so buy all num6packs for remaining nuggets.
else buy one num20pack, and call the buyChicken function recursively.
Following is the function buyChicken in C++
void buyChicken(unsigned n, unsigned &num6packs, unsigned &num9packs , unsigned &num20packs)
{ //base case
if(n==0)
return;
//if n is divisble by 6
if(n%6==0)
{
num6packs = num6packs+n/6;
return;
}
//if n is divisible by 9
else if(n%9==0)
{
num9packs = num9packs+n/9;
return;
}
//if n is divisible by 3
else if(n%3==0)
{
num9packs++;
num6packs=num6packs+(n-9)/6;
return;
}
//otherwise include a num20pack
else {
num20packs=num20packs+1;
buyChicken(n-20, num6packs, num9packs , num20packs);
}
}
Following is the complete C++ program to check the same :
#include <iostream>
using namespace std;
void buyChicken(unsigned n, unsigned &num6packs, unsigned &num9packs , unsigned &num20packs)
{ //base case
if(n==0)
return;
//if n is divisble by 6
if(n%6==0)
{
num6packs = num6packs+n/6;
return;
}
//if n is divisible by 9
else if(n%9==0)
{
num9packs = num9packs+n/9;
return;
}
//if n is divisible by 3
else if(n%3==0)
{
num9packs++;
num6packs=num6packs+(n-9)/6;
return;
}
//otherwise include a num20pack
else {
num20packs=num20packs+1;
buyChicken(n-20, num6packs, num9packs , num20packs);
}
}
int main()
{
int n;
unsigned int num6packs=0,num9packs=0,num20packs=0;
cout<<"Enter n : ";
cin>>n;
buyChicken(n, num6packs, num9packs , num20packs);
cout<<"num6packs : "<<num6packs<<endl;
cout<<"num9packs : "<<num9packs<<endl;
cout<<"num20packs : "<<num20packs;
return 0;
}
Code demo for reference :
Output :