Problem Statement In a restaurant, if you were pleased by the waiter\'s service,
ID: 3786950 • Letter: P
Question
Problem Statement
In a restaurant, if you were pleased by the waiter's service, you may leave him a tip -- you pay him more than the actual value of the bill, and the waiter keeps the excess money. In some countries, not leaving a tip for the waiter is even considered impolite. During my recent holiday I was having dinner in a foreign restaurant. The pamphlet from my travel agency informed me that the proper way of tipping the waiter is the following:
The sum I pay must be round, i.e., divisible by 5.
The tip must be between 5% and 10% of the final sum I pay, inclusive.
Clearly, sometimes there may be multiple "correct" ways of settling the bill. I'd like to know exactly how many choices I have in a given situation. I could program it easily, but I was having a holiday... and so it's you who has to solve this task. You will be given:
an int bill -- the amount I have to pay for the dinner
an int cash -- the amount of money I have in my pocket
Write a function that computes how many different final sums satisfy the conditions above.
Constraints
Assume that both bill and cash are in dollars.
All the money I have is in one-dollar banknotes.
My Code:
int possible_payments(int bill, int cash) {
// fill in code here
int choices;
int counter = 0;
int leftoverCash = cash - bill;
int i = 0;
for (i; i < leftoverCash; i++){
int tipPercent = (i/bill)*100;
int total = i + bill;
if((total % 5 == 0) && (tipPercent <= 10) && (tipPercent >= 5)){
counter++;
}
}
return choices;
}
Explanation / Answer
Note : - I have modified your code according to requirements of the problem statement. Please check.
double percent(int t,int b) {
return (double)(t-b) / (double)t;
}
public int possiblePayments(int bill, int cash)
{
int total = bill;
int choices = 0;
while(total % 5 != 0)
total++;
if(total == bill)
total = total + 5;
while(total <= cash && percent(total,bill) <= 0.1) && (percent(total,bill) >= 0.05) {
choices++;
total = total + 5;
}
return choices;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.