Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Distribution until Zero - Easy way

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning erveryone,

I am currently working on a project for myself but I got stuck during the exercise.

Inside the project, there is a task where we need to distribute a certain value to different variables until zero.
For example, I have 3000 units rice and want to sell it for different prices.
Best price 4$ then 3$, 2$ etc.

The 4$ market will sometimes by more sometimes less but has priority, the rest gets sold for 3$ first and then for 2$ until nothing is left.
I am trying to find a nice way to put this in an algorithm for hours now but can't find a nice way.

Can somebody give me a hint?

Help will be highly appreciated!
 
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yannick Mismahl wrote:Best price 4$ then 3$, 2$ etc.


Most consumers in Europe would disagree with you on this one, but I'm sure manufacturers would stand by you. .. And welcome to the Ranch

Are you trying to come up with an algorithm on a piece of paper? That's probably where you supposed to start at.
 
Saloon Keeper
Posts: 6419
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow your algorithm. If I have 3000units, and the best price is $4, then why don't I sell all 3000 @ $4 ?
 
Michael Grünau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thanks for the quick feedback.

I am trying to get an algorithm but it should be very basic.
But I am not getting any good ideas to solve the problem.

Basic idea is that my amount of goods and the demand will change.
The 4$ market will usually never buy all my goods so the 3$ and 2$ will get the rest, like below;

Units: 4000

market 4$ = 2500 Units
market 3$ = 1000 Units
market 2$ =500 Units

At the end nothing is left.

I have tried to formulate a loop or some if/else commands but they usually end up pretty useless.
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How and when price get reduced?
 
Michael Grünau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The price does not get reduced, I will always prefer the 4$ option then 3$ and afterwards 2$.
Only my amount of units will change and the demand each market has.

I always want to give the full demand of units to the first market and check if there is still something left for the second, third...
The algorithm should show how much demand was fulfilled and how much is left.
 
Sheriff
Posts: 6374
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I can help you get started.  You might start with:

1) Buy as much $4 units as you can
2) if there's money left over, buy as many $3 units as you can
3) If there's money left over, by as many $2 units as you can.

Now, how would you do the first part?

* multiply the $4 demand by the demand units, Getting extension
* if extension if less than the money you have, buy all the demand and subtract the extension from the total money
* else ?

Does that get you started?
 
Yup, yup, yup. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!