SUGARCANE - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: Lavish Gupta
Testers: Satyam, Abhinav Sharma
Editorialist: Nishank Suresh

DIFFICULTY:

To be calculated

PREREQUISITES:

Basic mathematical reasoning

PROBLEM:

A sugarcane juicer sells each glass of juice for 50 coins. 20\% of his income is spent on buying sugarcane, 20\% is spent on salt and mint leaves, and a further 30\% is spent on shop rent.

If he sells a total of N glasses of juice, what is his overall profit?

EXPLANATION:

Let’s look at what happens when N = 1. The juicer sells one glass of juice for 50 coins. Then,

  • He spends 20\% of 50 on sugarcane, which is 10 coins.
  • Another 20\% is spent on salt and mint, which is another 10 coins.
  • 30\% is spent on rent, which is 15 coins.

So, his total costs come out to be 10 + 10 + 15 = 35, and hence his profit is 50 - 35 = 15 coins.

Now note that the same calculation applies to every glass sold, so he earns a profit of 15 coins per glass sold. So, his final profit is 15\cdot N coins.

TIME COMPLEXITY:

\mathcal{O}(1) per test.

CODE:

Python
for _ in range(int(input())):
	print(15*int(input()))
1 Like

Why cant we use cout<<0.30*(50*n)<<endl; ?

The formula is technically correct (since 0.3 \cdot 50 = 15), the issue with doing it this way is the output format and precision issues.

For example, for input

1
599351

that code prints 8.99026e+06 when it should be printing 8990265.

In the future, I recommend working only with integers if it’s possible to do so — using floats and doubles has a tendency to cause some weird issues unless you know what you’re doing.

2 Likes

Thank you verymuch for giving the reason along with an example. Now i clearly understood the reason.

Yup, i will try to just work with int if i can. Thanks once again.