TCS NQT - Coding Ability

Test Number 4



1 .Problem Statement

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 and j Peanuts. If total m number of Bananas and p number of Peanuts are offered by travelers, calculate how many Monkeys remain on the Tree after some of them jumped down to eat.
At a time one Monkeys 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 or j 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 code to take inputs as n, m, p, k, j and return the number of Monkeys left on the Tree.
Where, n= Total no of Monkeys
k= Number of eatable Bananas by Single Monkey (Monkey that jumped down last may get less than k Bananas)
j = Number of eatable Peanuts by single Monkey(Monkey that jumped down last may get less than j Peanuts)
m = Total number of Bananas
p = Total number of Peanuts
Remember that the Monkeys always eat Bananas and Peanuts, so there is no possibility of k and j having a value zero

Example 1:
Input Values
20
2
3
12
12

Output Values
Number of Monkeys left on the tree:10

Note: Kindly follow the order of inputs as n,k,j,m,p as given in the above example. And output must include the same format as in above example(Number of Monkeys left on the Tree:)
For any wrong input display INVALID INPUT

#include <stdio.h>
int main()
{
int n,k,j,m,p;
float atebanana=0.0,atepeanut=0.0;
scanf("%d %d %d %d %d",&n,&k,&j,&m,&p);
if(n<0 || k<0 || j<0 || m<0 || p<0)
{
printf("INVALID INPUT");
}
else
{
if(k>0)
atebanana =(float)m/k;
if(j>0)
atepeanut =(float) p/j;
n=n-atebanana-atepeanut;
printf("Number of Monkeys left on the Tree:%d",n);
}
return 0;

}

import java.util.*;
class Monkeys
{
public static void main(String []args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int j = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt();
int atebanana=0 ,atepeanut=0;
if( n<0 && k<0 || j<0 || m<0 || p<0)
{
System.out.println("Invalid Input");
}
else
{
if(k>0)
atebanana =m/k;
if(j>0)
atepeanut = p/j;
n=n-atebanana-atepeanut;
System.out.println("Number of Monkeys left on the Tree: "+n);
}
}

}





";

2.Problem Statement

Chain Marketing Organization has has a scheme for income generation, through which its members generate income for themselves. The scheme is such that suppose A joins the scheme and makes R and V to join this scheme then A is Parent Member of R and V who are child Members. When any member joins the scheme then the parent gets total commission of 10% from each of its child members.
Child members receive commission of 5% respectively. If a Parent member does not have any member joined under him, then he gets commission of 5%.
Take name of the members joining the scheme as input.
Display how many members joined the scheme including parent member.Calculate the Total commission gained by each members in the scheme. The fixed amount for joining the scheme is Rs.5000 on which commission will be generated
SchemeAmount = 5000

Example 1: When there are more than one child members
Input : (Do not give input prompts.Accept values as follows. )
Amit //Enter parent Member as this
Y //Enter Y if Parent member has child members otherwise enter N
Rajesh,Virat //Enter names of child members of Amit in comma separated
Output:(Final Output must be in format given below.)
TOTAL MEMBERS:3
COMISSION DETAILS
Amit: 1000 INR
Rajesh :250 INR
Virat: 250 INR

Example 2: When there is only one child member in the hierarchy
Input :
Amit
Y
Rajesh
Output:
Total Members: 2
Comission Details
Amit: 500 INR
Rajesh: 250 INR

   

using namespace std;
int main()
{
string par;
cin >> par;
string x;
cin >> x;
if (x == "N") {
cout << "TOTAL MEMBERS:1\n";
cout << "COMISSION DETAILS\n";
cout << par << ":250 INR\n";
} else {
string child;
cin >> child;
vector<string>v;
string temp = "";
for (int i = 0; i < child.length(); i++) {
if (child[i] == ',') {
v.push_back(temp);
temp = "";
}
else if (child[i] != ' ')
temp += child[i];
}
v.push_back(temp);
cout << "TOTAL MEMBERS:" << v.size() + 1 << "\n";
cout << "COMISSION DETAILS\n";
cout << par << ":" << v.size() * 500 << " INR\n";
for (auto a : v) {
cout << a << ":" << "250 INR\n";
}
}
}

       

parent = input()
Yes_No = input()
if Yes_No == "N":
print("TOTAL MEMBERS:1\nCOMMISSION DETAILS\n{0}: 250 INR".format(parent))
elif Yes_No == "Y":
child=list(map(str,input().split(",")))
print("TOTAL MEMBERS:{}".format(len(child)+1))
print("COMMISSION DETAILS \n{0}:{1} INR".format(parent,len(child)*500))
for i in child:
print("{0}:250 INR".format(i))