A beer problem (divide bills)

123 Views Asked by At

I am making an application to divide bills. I have the following problem:

Jhon and Paul were to a pub , and they consumed 20 beers. James get in the bar and they consumed ten more beers.

How much Paul have to pay ?

I tried this approach:

Jhon and Paul drank all beers so they have to pay all of them = 1

James just drink 1/3 of the beers.

So james have to pay 30/(1 + 1 + 0,33) = 30/2,33 = 12,87

I know the correct result is 13,33

What i'm doing wrong?

EDIT

I know i can calculate the price like 20/2 + 10/3. But i'm doing an algorithm and this is hard to implement that.

That's why i wanna know what i'm doing wrong.

EDIT

The correct formula is:

James drank 1/3 of the beers, but have other 2 people drank with him so, it's not 10/30 its (10 - 2,33) / 30 = 0,25

Paul have to pay 30 / (1 + 1 + 0,25)

2

There are 2 best solutions below

4
On BEST ANSWER

So Paul has to pay 10 beers (20/2 because there were only Paul and John at the beginning) and "3.33" because James came there so there were Paul,John and James now and they drank 10 beers so - $10/3 = 3.33$ .

$10+3.33 = 13.33$

For algorithm all you have to do is implement following equation in cycle, where in every step of cycle you will input new values for $b$ and $n$ and before cycle give $S=0$ .

$S = S+\frac bn$

where

S - sum of bills
b - number of beers in one "cycle" (20 at beginning, then 10 when james came etc.)
n - number of people

2
On

Assuming they all drank exactly the same ammount of beer that were served to them:

== Each of Jhon and Paul drank 10 + 3.333 = 13.333 beers and this each must pay.

== James drank 3.333 beers and this he pays.