There are total n number of Monkeys sitting on
the branches of a huge Tree. As travelers offer
Bananas and Peanuts, the Monkeys jump down
the Tree.
If every Monkey can eat k Bananas andj
Peanuts. If Total m number of Bananas and p
number of Peanuts offered by Travelers,
calculate how many Monkeys remain on the
Tree after some of them jumped down to eat.
At a time, one Monkey gets down and finishes
eating and go to the other side of the road. The
Monkey who climbed down does not climb up
again after eating until the other Monkeys finish
eating.
Monkey can either eat k Bananas orj Peanuts. If
for last Monkey there are less than k Bananas
left on the ground or less than j Peanuts left on
the ground, only that Monkey can eat
Bananas(<k) along with the Peanuts(<j).
Write the code to take inputs as n, m, p, k, j and
return the number of Monkeys left on the Tree.
Where, n = Total number of Monkeys
Answers
Answer:
def monkeysleft(n, b, p, k, j):
c=n
for i in range(0,n):
if(c==0 or p<=0|b<=0):
print("ended")
return(0)
if(p<=j or b<=k):
print("ended")
return(c-1)
else:
p=p-j
b=b-k
c=c-1
\\n,b,p,k,j=(10,20,40,5,5)
print(n,b,p,k,j)
print(monkeysleft(n,b,p,k,j))
Explanation:
-----
Program in C++
#include<iostream>
using namespace std;
int main()
{
int n , m , p , k , j;
cout<<"Enter total number of monkeys : ";
cin>>n;
cout<<"Enter total number of bananas : ";
cin>>m;
cout<<"Enter total number of peanuts : ";
cin>>p;
cout<<"Enter number of bananas a monkey can eat : ";
cin>>k;
cout<<"Enter number of peanuts a monkey can eat : ";
cin>>j;
if(n>0 && m>0 && p>0 && k>0 && j>0)
{
int n1 = m / k;
int b_left = m % k;
int n2 = p / j;
int p_left = p % j;
int n3 = n - n1 - n2;
if(n3==1 && (b_left<k || p_left<j))
{
cout<<"Number of monkeys left on the tree : 0";
}
else
{
cout<<"Number of monkeys left on the tree : "<<n3;
}
}
else
{
cout<<"Invalid Input";
}
return 0;
}
Output:
Enter total number of monkeys : 20
Enter total number of bananas : 12
Enter total number of peanuts : 12
Enter number of bananas a monkey can eat : 2
Enter number of peanuts a monkey can eat : 3
Number of monkeys left on the tree : 10