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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Distribution until Zero - Easy way

Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
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: 8493
610
• 1
• Number of slices to send:
Optional 'thank-you' note:

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: 9714
79
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
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: 8493
610
• Number of slices to send:
Optional 'thank-you' note:
How and when price get reduced?

Michael Grünau
Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
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: 7123
184
• Number of slices to send:
Optional 'thank-you' note:

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?

 money grubbing section goes here: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising